python - 有没有一种简单的方法可以从文本文件读取到这个漂亮的汤库 python 脚本?
问题描述
如何将 txt.file 中的行读入此脚本,而不必在脚本中列出 url?谢谢
from bs4 import BeautifulSoup
import requests
url = "http://www.url1.com"
response = requests.get(url)
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)
我的 text.file 内容有一个换行符的分隔符:
http://www.url1.com
http://www.url2.com
http://www.url3.com
http://www.url4.com
http://www.url5.com
http://www.url6.com
http://www.url7.com
http://www.url8.com
http://www.url9.com
解决方案
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 变量替换你的行
推荐阅读
- python - 在python中使用opencv剪切图像的特定部分
- flutter - 如何让切换按钮占据容器中的整个空间?
- corda - 当我在不同的机器上执行交易时,Corda 节点正在收集签名
- php - 为什么每次我刷新页面时都会在数据库中发布一个新会话?
- linux - 问:bash 脚本中的“-”是什么意思?
- algorithm - 有没有办法在树上找到路径?
- javascript - 在多个异步 useState 设置器之后在 React 中运行一些代码
- cuda - 在 asm volatile 内联 PTX 指令中,为什么还要指定“内存”副作用?
- java - 当 Profiler 启动时,Android 应用程序给出“JVMTI 错误:103(JVMTI_ERROR_ILLEGAL_ARGUMENT)”
- svg - svg 可拖动/全景缩放的意外行为