1 回答
TA貢獻(xiàn)1790條經(jīng)驗(yàn) 獲得超9個(gè)贊
你能更清楚你想要什么嗎?
這是一個(gè)可能的答案,可能是也可能不是您想要的:
from bs4 import BeautifulSoup
import requests
with open('yourfilename.txt', 'r') as url_file:
for line in url_file:
u = line.strip()
response = requests.get(u)
data = response.text
soup = BeautifulSoup(data,'lxml')
文件是用open()函數(shù)打開的;第二個(gè)參數(shù)是'r'指定我們以只讀模式打開它。對(duì) 的調(diào)用open()被封裝在一個(gè)with塊中,因此一旦您不再需要打開文件,文件就會(huì)自動(dòng)關(guān)閉。該strip()函數(shù)刪除每行開頭和結(jié)尾的尾隨空格(空格、制表符、換行符),立即' https://stackoverflow.com '.strip()變?yōu)?#39;https://stackoverflow.com'.
添加回答
舉報(bào)
