I'm using python 3.5 with bs 4.6, selenium 3.6 and phantomjs to scrape this one site. The script runs on my server which is located in the US and I want to scrape a german site. But I ran into kind of a problem. The html I download looks like this:
<div class="col-md-40 product-highlights-container"><div class="product-filters"><select class="colorfilter__select"><option value="{"ebootisId":"HW102581-1","color":"Midnight Black","colorCode":"000000","colorGroup":"Schwarz","colorGroupCode":"000000","deliveryTime":"2-3 Werktage","default":true,"images":[{"small":"/img/dist/HW102581-1_ZU102869_S_1.png","medium":"/img/dist/HW102581-1_ZU102869_M_1.png","large":"/img/dist/HW102581-1_ZU102869_L_1.png"}],"storage":"64","tariffs":{"TF102910":{"ebootisId":"TF102910","price":49,"url":"/smartphones/samsung/galaxy-s8-inkl-gear-sport?farbe=midnight-black&speicher=64&carrier=vodafone&tarif=comfort-allnet"},"TF101415":{"ebootisId":"TF101415","price":49,"url":"/smartphones/samsung/galaxy-s8-inkl-gear-sport?farbe=midnight-black&speicher=64&carrier=telekom&tarif=comfort-allnet"}},"stock":1086,"url":"/smartphones/samsung/galaxy-s8-inkl-gear-sport?farbe=midnight-black&speicher=64&carrier=vodafone&tarif=comfort-allnet","price":49,"offer_id":"5a8bf20d56b4537a4076868a","soldout":false}">Midnight Black</option><option value="{"ebootisId":"HW102581-2","color":"Arctic Silver","colorCode":"c7ccd0","colorGroup":"Silber","colorGroupCode":"c0c0c0","deliveryTime":"2-3 Werktage","default":false,"images":[{"small":"/img/dist/HW102581-2_ZU102869_S_1.png","medium":"/img/dist/HW102581-2_ZU102869_M_1.png","large":"/img/dist/HW102581-2_ZU102869_L_1.png"}],"storage":"64","tariffs":{"TF102910":{"ebootisId":"TF102910","price":49,"url":"/smartphones/samsung/galaxy-s8-inkl-gear-sport?farbe=arctic-silver&speicher=64&carrier=vodafone&tarif=comfort-allnet"},"TF101415":{"ebootisId":"TF101415","price":49,"url":"/smartphones/samsung/galaxy-s8-inkl-gear-sport?farbe=arctic-silver&speicher=64&carrier=telekom&tarif=comfort-allnet"}},"stock":503,"url":"/smartphones/samsung/galaxy-s8-inkl-gear-sport?farbe=arctic-silver&speicher=64&carrier=vodafone&tarif=comfort-allnet","price":49,"offer_id":"5a8bf20d56b4537a4076868a","soldout":false}">Arctic Silver</option><option value="{"ebootisId":"HW102581-3","color":"Orchid Grey","colorCode":"9d9dad","colorGroup":"Grau","colorGroupCode":"dcdcdc","deliveryTime":"2-3 Werktage","default":false,"images":[{"small":"/img/dist/HW102581-3_ZU102869_S_1.png","medium":"/img/dist/HW102581-3_ZU102869_M_1.png","large":"/img/dist/HW102581-3_ZU102869_L_1.png"}],"storage":"64","tariffs":{"TF102910":{"ebootisId":"TF102910","price":49,"url":"/smartphones/samsung/galaxy-s8-inkl-gear-sport?farbe=orchid-grey&speicher=64&carrier=vodafone&tarif=comfort-allnet"},"TF101415":{"ebootisId":"TF101415","price":49,"url":"/smartphones/samsung/galaxy-s8-inkl-gear-sport?farbe=orchid-grey&speicher=64&carrier=telekom&tarif=comfort-allnet"}},"stock":500,"url":"/smartphones/samsung/galaxy-s8-inkl-gear-sport?farbe=orchid-grey&speicher=64&carrier=vodafone&tarif=comfort-allnet","price":49,"offer_id"
It is basically one long line of text, which makes it impossible for me to find the tags I want to find.
If I am using an online beautifier or splitting up the lines myself it works fine, but that's not a viable solution.
I tried to use the prettify()
function from bs4 but that didnt work either.
Thats the relevant piece of code:
driver = webdriver.PhantomJS(executable_path = path_to_pjs)
driver.get(link)
f = open(filename, "wb")
f.write(driver.page_source.encode('utf-8'))
f.close()
driver.close()
ecj_data = open(filename ,'r', encoding='utf-8').read()
page_soup = soup(ecj_data,"lxml")
page_soup=page_soup.prettify()
The code you have could be changed as follows. It will create an output file called pretty.html
containing the prettify
version of the HTML:
from bs4 import BeautifulSoup
from selenium import webdriver
link = 'https://tarife.mediamarkt.de/smartphones/samsung/galaxy-s8-inkl-gear-sport?farbe=midnight-black&speicher=64&carrier=vodafone&tarif=comfort-allnet'
filename = 'output.html'
driver = webdriver.PhantomJS() #executable_path=path_to_pjs)
driver.get(link)
with open(filename, "wb") as f_output:
f_output.write(driver.page_source.encode('utf-8'))
page_soup = BeautifulSoup(driver.page_source, "lxml")
with open('pretty.html', 'w') as f_output:
f_output.write(page_soup.prettify())
driver.close()
Giving you a <div>
starting:
<div class="col-md-40 product-highlights-container">
<div class="product-filters">
<select class="colorfilter__select">
<option value='{"ebootisId":"HW102581-1","color":"Midnight Black","colorCode":"000000","colorGroup":"Schwarz","colorGroupCode":"000000","deliveryTime":"2-3 Werktage","default":true,"images":[{"small":"/img/dist/HW102581-1_ZU102869_S_1.png","medium":"/img/dist/HW102581-1_ZU102869_M_1.png","large":"/img/dist/HW102581-1_ZU102869_L_1.png"}],"storage":"64","tariffs":{"TF102910":{"ebootisId":"TF102910","price":49,"url":"/smartphones/samsung/galaxy-s8-inkl-gear-sport?farbe=midnight-black&speicher=64&carrier=vodafone&tarif=comfort-allnet"},"TF101415":{"ebootisId":"TF101415","price":49,"url":"/smartphones/samsung/galaxy-s8-inkl-gear-sport?farbe=midnight-black&speicher=64&carrier=telekom&tarif=comfort-allnet"}},"stock":1075,"url":"/smartphones/samsung/galaxy-s8-inkl-gear-sport?farbe=midnight-black&speicher=64&carrier=vodafone&tarif=comfort-allnet","price":49,"offer_id":"5a8bf20d56b4537a4076868a","soldout":false}'>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.