1 回答
TA贡献2016条经验 获得超9个赞
始终使用硒作为节省资源的最后手段。
from selenium import webdriver
url = 'https://www.who.int/csr/disease/coronavirus_infections/faq_dec12/en/'
driver = webdriver.Chrome()
try:
driver.get(url)
div_text = driver.find_element_by_id('primary').text
with open('website_content.txt','w') as f:
f.write(div_text)
except Exception as e:
print(e)
finally:
if driver is not None:
driver.close()
你可以通过要求和美丽的汤来实现同样的事情,如下所示:
import requests as rq
from bs4 import BeautifulSoup
response = rq.get(url)
if response.status_code == 200:
soup = BeautifulSoup(response.text,'html.parser')
div_text = soup.find('div',{'id':'primary'}).text
with open('website_content.txt','w') as f:
f.write(div_text)
添加回答
举报