第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

美麗的湯選擇器返回一個(gè)空列表

美麗的湯選擇器返回一個(gè)空列表

MMMHUHU 2023-12-19 21:39:16
所以我正在做自動(dòng)化無(wú)聊的東西課程,我試圖抓取自動(dòng)化無(wú)聊的東西書(shū)的亞馬遜價(jià)格,但無(wú)論如何它都會(huì)返回一個(gè)空字符串,因此在 < /span>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)
查看完整描述

2 回答

?
繁華開(kāi)滿天機(jī)

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超4個(gè)贊

您需要將解析器更改為 lxml 并使用 headers = {'user-agent': 'Mozilla/5.0'}


def getAmazonPrice(productUrl):

    headers = {'user-agent': 'Mozilla/5.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, 'lxml')

    elems = soup.select_one('#mediaNoAccordion > div.a-row > div.a-column.a-span4.a-text-right.a-span-last')

    return elems.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)

快照:

https://img1.sycdn.imooc.com/65819d22000178df18250660.jpg

如果你想使用選擇然后


def getAmazonPrice(productUrl):

    headers = {'user-agent': 'Mozilla/5.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, 'lxml')

    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)

嘗試用這個(gè)。


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, 'lxml')

    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)


查看完整回答
反對(duì) 回復(fù) 2023-12-19
?
函數(shù)式編程

TA貢獻(xiàn)1807條經(jīng)驗(yàn) 獲得超9個(gè)贊

您的請(qǐng)求將觸發(fā)亞馬遜的 503 錯(cuò)誤。也許是由于亞馬遜的反抓取努力。所以也許你應(yīng)該考慮一些其他的方法。


import requests


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


productUrl = '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'


res = requests.get(productUrl, headers=headers)


print (res)

輸出:


<Response [503]>


查看完整回答
反對(duì) 回復(fù) 2023-12-19
  • 2 回答
  • 0 關(guān)注
  • 262 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)