我有一个独特的问题,我以为我已经解决了,直到我撞到墙使用While循环来控制这个程序的流程。你知道吗
简介:
我有一个平面文件(CSV或文本),其中有一些我想删除的URL,使用BeautifulSoup在HTML中添加一个新的标记(有效),然后将每个删除的URL保存到一个新的文件名中。你知道吗
我需要的是:
我很确定这和我不能理解基本的东西有关,我仍然在努力解决这个问题。这是我的密码:
怎么了:
使用Python3,代码实际上是可以工作的,我使用Jupyter逐行观察代码和一系列print语句,以查看While循环运行时get返回了什么。你知道吗
问题是只保存了一个文件,文件末尾的URL是唯一保存的内容。其他URL被刮除。你知道吗
在进入下一行之前,如何对每一行进行迭代和刮取以唯一地保存?我是否错误地使用了这些结构?你知道吗
网址:
https://www.imgacademy.com/media/headline/img-academy-u19-girls-win-fysa-state-cup-u19-championship
代码:
import csv
import requests
from bs4 import BeautifulSoup as BS
filename = 'urls.csv'
with open(filename, 'r+') as file:
while True:
line = file.readline()
user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0'
headers = {'User-Agent':user_agent}
response = requests.get(line, headers)
print(response)
soup = BS(response.content, 'html.parser')
html = soup
title = soup.find('title')
meta = soup.new_tag('meta')
meta['name'] = "robots"
meta['content'] = "noindex, nofollow"
title.insert_after(meta)
for i
with open('{}'".txt".format("line"), 'w', encoding='utf-8') as f:
outf.write(str(html))
if (line) == 0:
break
相关问题 更多 >
编程相关推荐