![](/img/trans.png)
[英]login to page with Selenium works - parsing with BS4 works - but not the combination of both
[英]Parsing text with bs4 works with selenium but does not work with requests in Python
這段代碼有效並返回我想要的個位數,但它太慢了,需要 10 秒才能完成。我將運行這 4 次供我使用,這樣每次運行都會浪費 40 秒。 ` 從 selenium 導入 webdriver 從 bs4 導入 BeautifulSoup
options = webdriver.FirefoxOptions()
options.add_argument('--headless')
driver = webdriver.Firefox(options=options)
driver.get('https://warframe.market/items/ivara_prime_blueprint')
html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')
price_element = soup.find('div', {'class': 'row order-row--Alcph'})
price2=price_element.find('div',{'class':'order-row__price--hn3HU'})
price = price2.text
print(int(price))
driver.close()`
另一方面,此代碼不起作用。 它返回無。 ` 導入請求來自 bs4 導入 BeautifulSoup
url='https://warframe.market/items/ivara_prime_blueprint'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
price_element=soup.find('div', {'class': 'row order-row--Alcph'})
price2=price_element.find('div',{'class':'order-row__price--hn3HU'})
price = price2.text
print(int(price))`
首先想到的是添加用戶代理,但仍然沒有用。 當我打印(湯)時,它給了我 html 代碼,但是當我進一步解析它時,它停止並開始給我 None 甚至它與 selenium 示例中的命令相同。
數據在<script>
標簽內動態加載,因此 Beautifulsoup 看不到它(它不呈現 Javascript)。
例如,要獲取數據,您可以使用:
import json
import requests
from bs4 import BeautifulSoup
url = "https://warframe.market/items/ivara_prime_blueprint"
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"
}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, "html.parser")
script_tag = soup.select_one("#application-state")
json_data = json.loads(script_tag.string)
# Uncomment the line below to see all the data
# from pprint import pprint
# pprint(json_data)
for data in json_data["payload"]["orders"]:
print(data["user"]["ingame_name"])
印刷:
Rogue_Monarch
Rappei
KentKoes
Tenno61189
spinifer14
Andyfr0nt
hollowberzinho
您可以將數據作為dict
訪問並訪問keys
/ values
。
我推薦一個在線工具來查看所有 JSON,因為它非常大。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.