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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何循環(huán)一個 div 并僅使用美麗湯和蟒蛇獲取段落標記中的文本?

如何循環(huán)一個 div 并僅使用美麗湯和蟒蛇獲取段落標記中的文本?

回首憶惘然 2022-09-20 15:30:32
我正在使用美麗湯和python來抓取網(wǎng)頁,并僅從網(wǎng)站中提取段落標簽中的文本。這是我想抓取的頁面我想要所有段落標簽中的所有文本。
查看完整描述

1 回答

?
慕沐林林

TA貢獻2016條經(jīng)驗 獲得超9個贊

始終使用硒作為節(jié)省資源的最后手段。


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()

你可以通過要求和美麗的湯來實現(xiàn)同樣的事情,如下所示:


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)


查看完整回答
反對 回復 2022-09-20
  • 1 回答
  • 0 關(guān)注
  • 81 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

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