from bs4 import BeautifulSoup
import requests
import random
id_url = "https://codeforces.com/profile/akash77"
id_headers = {
"User-Agent": 'Mozilla/5.0(Windows NT 6.1Win64x64) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 87.0 .4280 .141 Safari / 537.36 '}
id_page = requests.get(id_url, headers=id_headers)
id_soup = BeautifulSoup(id_page.content, 'html.parser')
id_soup = id_soup.find('svg')
print(id_soup)
我得到None了這個輸出。
如果我決議包含<div>此<svg>標記的<div>元素,則不會列印該元素的內容。該find()工程除SVG標記的所有HTML標簽。
uj5u.com熱心網友回復:
該網頁是使用 Javascript 動態呈現的,因此您將需要
有時,您需要在無頭模式下運行瀏覽器(無需打開 chrome UI),因為您可以將“無頭”選項傳遞給驅動程式。
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('headless')
# then pass options to the driver
driver = webdriver.Chrome(service=s, options=options)
uj5u.com熱心網友回復:
svg 標簽不包含在源代碼中,它是由 Javascript 呈現的。
uj5u.com熱心網友回復:
如果您只想要它在 html 中的資料,這并不漂亮,但它比瀏覽器自動化更快速、更容易:
import requests
import json
url = 'https://codeforces.com/profile/akash77'
resp = requests.get(url)
start = "$('#userActivityGraph').empty().calendar_yearview_blocks("
end = "start_monday: false"
s = resp.text
svg_data = s[s.find(start) len(start):s.rfind(end)].strip()[:-1].replace('items','"items"').replace('data','"data"').replace('\n','').replace('\t','').replace(' ','') #get the token out the html
broken = svg_data '}'
json_data = json.loads(broken)
print(json_data)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/408310.html
標籤:
