beautifulsoup multiple keyword from csv file
beautifulsoup multiple keyword from csv file
I have a csv file with 2 column A and B, and I want scrap all the file with beautifulsoup
The url is composed like this : http://.../search?info=A&who=B
how to create a loop?
my code
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
And why would you need beautifulsoup if the response is a csv already. Please give a sample output of that request and what you are trying to do.
– Ilhicas
Jun 29 at 17:46
show me your csv a couple of lines like 5 maybe
– Kostadin Slavov
Jun 29 at 18:04
It's better to use
.DictReader()
in order to be able to call the desired items using the header
like row['header']
instead of using that hardcoded way.– SIM
Jun 29 at 21:41
.DictReader()
header
row['header']
1 Answer
1
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
i have update my code and i have File "jo.py", line 8, in <module> for row in reader: ValueError: I/O operation on closed file
– jarodfrance
Jun 29 at 20:30
It was not indented. I have edited it. can you check now? for statement should be inside the with block
– Linda
Jun 29 at 20:35
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Why do you need a loop?
– Mad Physicist
Jun 29 at 17:41