So I have been working out with abit of bs4 and managed to print out a text. Right now I managed to print out var ajaxsearch
which init comes alot more.
I have written a code where it prints out all that contains javascript and print out where var ajaxsearch
starts withit:
try:
product_li_tags = bs4.find_all('script', {'type': 'text/javascript'})
except Exception:
product_li_tags = []
special_code = ''
for s in product_li_tags:
if s.text.strip().startswith('var ajaxsearch'):
special_code = s.text
break
print(special_code)
and I am getting an output of:
var ajaxsearch = false;
var combinationsFromController ={
"224114": {
"attributes_values": {
"4": "5.5"
},
"attributes": [
22
],
"unit_impact": 0,
"minimal_quantity": "1",
"date_formatted": "",
"available_date": "",
"id_image": -1,
"list": "'22'"
},
"224140": {
"attributes_values": {
"4": "6"
},
"attributes": [
23
],
"unit_impact": 0,
"minimal_quantity": "1",
"date_formatted": "",
"available_date": "",
"id_image": -1,
"list": "'23'"
},
"224160": {
"attributes_values": {
"4": "6.5"
},
"attributes": [
24
],
"unit_impact": 0,
"minimal_quantity": "1",
"date_formatted": "",
"available_date": "",
"id_image": -1,
"list": "'24'"
},
"224139": {
"attributes_values": {
"4": "7"
},
"attributes": [
25
],
"unit_impact": 0,
"minimal_quantity": "1",
"date_formatted": "",
"available_date": "",
"id_image": -1,
"list": "'25'"
},
"224138": {
"attributes_values": {
"4": "7.5"
},
"attributes": [
26
],
"unit_impact": 0,
"minimal_quantity": "1",
"date_formatted": "",
"available_date": "",
"id_image": -1,
"list": "'26'"
},
"224113": {
"attributes_values": {
"4": "8"
},
"attributes": [
27
],
"unit_impact": 0,
"minimal_quantity": "1",
"date_formatted": "",
"available_date": "",
"id_image": -1,
"list": "'27'"
},
"224129": {
"attributes_values": {
"4": "8.5"
},
"attributes": [
28
],
"unit_impact": 0,
"minimal_quantity": "1",
"date_formatted": "",
"available_date": "",
"id_image": -1,
"list": "'28'"
},
"224161": {
"attributes_values": {
"4": "9"
},
"attributes": [
29
],
"unit_impact": 0,
"minimal_quantity": "1",
"date_formatted": "",
"available_date": "",
"id_image": -1,
"list": "'29'"
}
};
var contentOnly = false;
var Blank = 1;
var Format = 2;
Meaning that when I print out s.text. I will get an output of the code above. Small edit: If I try to do if s.text.strip().startswith('var combinationsFromController'):
it won't find the value and also if I change it the other way around if 'var combinationsFromController' in s.text.strip():
it will print out the same output as above.
However my issue is that I just want to be able to print out var combinationsFromController
and skip the rest where I later on can convert the values to a json using json.loads but before that my issue is, How can I print so I can managed to just have the value var combinationsFromController
?
EDIT: probably solved it!
for s in product_li_tags:
if 'var combinationsFromController' in s.text.strip():
for line in s.text.splitlines():
if line.startswith('var combinationsFromController'):
get_full_text = line.strip()
get_config = get_full_text.split(" = ")
cut_text = get_config[1][:-1]
get_json_values = json.loads(cut_text)