简体   繁体   中英

Python BeautifulSoup unable to find the correct data; invalid syntax error for “class”

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM