Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

web scraping - Python Webscraping not return the right text, and sometimes no text at all

I'm trying to retrieve the price of the item from this amazon page, URL:

 https://www.amazon.com/FANMATS-University-Longhorns-Chrome-Emblem/dp/B00EPDLL6U/

Source Code

from bs4 import BeautifulSoup
import requests

text = "https://www.amazon.com/FANMATS-University-Longhorns-Chrome-Emblem/dp/B00EPDLL6U/"
page = requests.get(text)

data = page.text

soup = BeautifulSoup(data, 'lxml')

web_text = soup.find_all('div')

print(web_text)

Everytime I run the program, I get an output of html that's nothing similar to that of the webpage, saying things like:

" Sorry! Something went wrong on our end. Please go back and try again..."

I'm not sure what I'm doing wrong, any help would be much appreciated. I'm new to python and webscraping so I'm sorry if my issue is super obvious. Thanks! :)

question from:https://stackoverflow.com/questions/65912664/python-webscraping-not-return-the-right-text-and-sometimes-no-text-at-all

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Website is serving content dynamically what request could not handle, use selenium instead:

from selenium import webdriver
from bs4 import BeautifulSoup

url = 'https://www.amazon.com/FANMATS-University-Longhorns-Chrome-Emblem/dp/B00EPDLL6U/'
driver = webdriver.Chrome('C:Program FilesChromeDriverchromedriver.exe')
driver.get(url)
time.sleep(3)

html = driver.page_source
soup = BeautifulSoup(html,'html.parser')
print(soup.select_one('span#priceblock_ourprice').get_text())
driver.close()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...