【发布时间】:2020-07-27 07:54:03
【问题描述】:
所以我在做自动化无聊的东西课程,我试图为自动化无聊的东西书刮亚马逊价格,但无论如何它都会返回一个空字符串,因此在 elems[0] 处发生索引错误].text.strip() 我不知道该怎么办
def getAmazonPrice(productUrl):
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0'} # to make the server think its a web browser and not a bot
res = requests.get(productUrl, headers=headers)
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, 'html.parser')
elems = soup.select('#mediaNoAccordion > div.a-row > div.a-column.a-span4.a-text-right.a-span-last')
return elems[0].text.strip()
price = getAmazonPrice('https://www.amazon.com/Automate-Boring-Stuff-Python-2nd-ebook/dp/B07VSXS4NK/ref=sr_1_1?crid=30NW5VCV06ZMP&dchild=1&keywords=automate+the+boring+stuff+with+python&qid=1586810720&sprefix=automate+the+bo%2Caps%2C288&sr=8-1')
print('The price is ' + price)
【问题讨论】:
-
另外,下载的 html 中包含的文本说“对不起,我们只需要确保您不是机器人。为了获得最佳效果,请确保您的浏览器接受 cookie。”,有一个提示你。
标签: python html python-3.x beautifulsoup python-requests