Reputation: 77
I've come here as a last resort. I cannot get this code to pull the correct data. It doesnt seem to be finding it at all. Please help.
Goal is to pull names and phone numbers off the website and put them into a CSV.
import requests
from bs4 import BeautifulSoup
import csv
def main(url):
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')
target = soup.select("div.result-item")
with open("Output.csv", 'a', newline="") as f:
writer = csv.writer(f)
writer.writerow(["Name", "Phone"])
for tar in target:
name = tar.find("div", class="result-name").text
phone = tar.find("div", class="result-phone").text
writer.writerow([name, phone])
urllink = "http://www.reinboundlogistics.com/cms/search-tool/3pl/"
main(urllink)
I get this result:
File "program1.py", line 13
name = tar.find("div", class="result-name").text
^
SyntaxError: invalid syntax
I cant seem to figure out why this syntax error is being thrown at me, because I've used almost identical code before successfully. The only difference being instead of "class=" I used "itemprop=".
Please advise on ways to improve, or improve my accuracy on pinpointing the data I want.
Upvotes: 1
Views: 654
Reputation: 33384
Class is the keyword.Change this to
name = tar.find("div", class_="result-name").text
phone = tar.find("div", class_="result-phone").text
OR
name = tar.find("div", attrs={"class" :"result-name"}).text
phone = tar.find("div", attrs={"class" :"result-phone"}).text
Upvotes: 1