Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 美化从csv文件中选择多个关键字_Python_Web Scraping_Beautifulsoup - Fatal编程技术网

Python 美化从csv文件中选择多个关键字

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?

我有一个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?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