python 我怎么能刮电话号码和网站地址使用美丽的汤

ie3xauqp  于 2023-01-12  发布在  Python
关注(0)|答案(1)|浏览(84)
import requests
from bs4 import BeautifulSoup

url = 'https://www.yellowpages.ca/search/si/1/coffee/Toronto+ON'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')

for listing in soup.find_all('div', class_='listings'):
    phone = listing.find('div', class_='phone')
    website = listing.find('div', class_='url')
    if phone:
        print(phone.text)
    if website:
        print(website.text)

我试图刮电话号码和网站地址从这个网站https://www.yellowpages.ca/search/si/1/coffee/Toronto+ON,但我的代码返回没有结果,所以我不能理解错误在哪里

eagi6jfj

eagi6jfj1#

问题是你没有在响应中找到正确的标签。我修改了你的代码:

import requests
import re
from urllib.parse import unquote
from bs4 import BeautifulSoup

url = 'https://www.yellowpages.ca/search/si/1/coffee/Toronto+ON'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')

for item in soup.find_all('div', {"class": "listing__content__wrap--flexed"}):
    phone = item.find('a', {"title": "Get the Phone Number"}).get('data-phone')
    website = item.find('a', {"title": re.compile(r'Business Website')}).get('href')
    website = unquote(unquote(website.split('redirect=')[1]))
    print(f"phone: {phone}")
    print(f"website: {website}")

输出为:

phone: 416-531-7755
website: http://sicilianicecream.com/sicilian-sidewalk-cafe
phone: 416-849-1499
website: http://carolescheesecakecafeyorkville.com/
phone: 647-388-9726
website: https://www.starbucks.ca/store-locator/store/1014678
phone: 416-363-8555
website: https://www.starbucks.ca/store-locator/store/1005445

相关问题