3 回答
TA贡献1784条经验 获得超9个赞
要从中读取 URL a.txt,您可以使用此脚本:
import requests
from bs4 import BeautifulSoup
with open('a.txt', 'r') as f_in:
for line in map(str.strip, f_in):
if not line:
continue
response = requests.get(line)
data = response.text
soup = BeautifulSoup(data, 'html.parser')
categories = soup.find_all("a", {"class":'navlabellink nvoffset nnormal'})
for category in categories:
print(url + "," + category.text)
TA贡献1921条经验 获得超9个赞
file1 = open('text.file', 'r')
Lines = file1.readlines()
count = 0
# Strips the newline character
for line in Lines:
print("Line{}: {}".format(count, line.strip()))
你只需用 url 变量替换你的行
TA贡献1777条经验 获得超10个赞
为了这个例子,假设您的文件名为urls.txt. 在 Python 中,打开文件并读取其内容非常容易。
with open('urls.txt', 'r') as f:
urls = f.read().splitlines()
#Your list of URLs is now in the urls list!
after只是告诉 Python 以'r'阅读'urls.txt'模式打开文件。如果您不需要修改文件,最好以只读模式打开它。f.read() 返回文件的全部内容,但它包含换行符 ( \n),因此splitlines()将删除这些字符并为您创建一个列表。
添加回答
举报