Python 美化从csv文件中选择多个关键字
我有一个csv文件,有两列a和B,我想用beautifulsoup删除所有文件 url的组成如下:Python 美化从csv文件中选择多个关键字,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我有一个csv文件,有两列a和B,我想用beautifulsoup删除所有文件 url的组成如下: 如何创建循环 我的代码 from bs4 import BeautifulSoup import requests import json import csv with open('input.csv') as csvfile: reader = csv.reader(csvfile) for row in reader: url = ".../search?
如何创建循环 我的代码
from bs4 import BeautifulSoup
import requests
import json
import csv
with open('input.csv') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
url = ".../search?info={}&who={}".format(row[0], row[1])
response = requests.get(url)
html = response.content
soup = BeautifulSoup(html, "html5lib")
for p in soup.find_all(class_="crd"):
b = p.find(class_="info")
if b['data-info'] is not None:
j = json.loads(b['data-info'])
data= p.h2.a.string
为什么需要循环?如果响应已经是csv,为什么需要beautifulsoup。请给出该请求的示例输出以及您正在尝试执行的操作。向我显示您的csv,例如5行。最好使用
.DictReader()
,以便能够使用标题调用所需的项目,例如行['header']
,而不是使用那种硬编码方式。我已经更新了我的代码,我有了文件“jo.py”,第8行,for reader:ValueError:I/O操作在关闭的文件上它没有缩进。我已经编辑了它。您现在可以检查吗?for语句应该在with块中
import csv
with open('input.csv') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
url = url = ".../search?info={}&who={}".format(row[0], row[1])
#rest of your logic