为了账号安全,请及时绑定邮箱和手机立即绑定

从这个网站上抓取地址和电话号码

从这个网站上抓取地址和电话号码

月关宝盒 2022-06-07 16:55:09
如何使用bs4和 pandas 库从和联系信息类中抓取数据并导出到 csv 文件?从这个网站?我需要有关如何从标签和联系信息类中抓取数据的帮助。import pandas as pdimport bs4import requestsimport refull_dict={'Title':[],'Description':[],'Address':[]}res=requests.get("https://cupcakemaps.com/cupcakes/cupcakes-near-me/p:2")   listings=soup.findAll(class_='media')for listing in listings:    listing_title=listing.find(True,{'title':True}).attrs['title']    listing_Description=listing.find('p',{'class':'summary-desc'})    listing_address=listing.find('p',{'class':'contact-`info'}).text=re.compile(r'[0-9]{0,4}')`
查看完整描述

1 回答

?
幕布斯7119047

TA贡献1794条经验 获得超8个赞

.strip()- Python 的内置函数用于从字符串中删除所有前导和尾随空格。

.to_csv()- 将对象写入逗号分隔值 (csv) 文件。

前任。


import pandas as pd

from bs4 import BeautifulSoup,Tag

import requests

import re


res=requests.get("https://cupcakemaps.com/cupcakes/cupcakes-near-me/p:2")

soup = BeautifulSoup(res.text,'lxml')

listings=soup.findAll(class_='media')

data = []

for listing in listings:

    listing_title=listing.find(True,{'title':True}).attrs['title']

    listing_Description=listing.find('p',{'class':'summary-desc'})


    if isinstance(listing_Description,Tag):

        listing_Description = listing_Description.text.strip()


    listing_address=listing.find('p',{'class':'contact-info'})


    if isinstance(listing_address,Tag):

        number_text = listing_address.text.strip()

        listing_address = ''.join(filter(str.isdigit,number_text))


    full_dict = {'Title': listing_title, 'Description': listing_Description, 'Address': listing_address}

    data.append(full_dict)


df = pd.DataFrame(data)

# saved data into csv file

df.to_csv("contact.csv")

print(df)

输出/输出:


                                               Title                                        Description     Address

0  Explore Category 'Anaheim CA Birthday Cupcakes...  Delectable Anaheim, CA - Delectable check out ...  7147156086

1  Explore Category 'Costa Mesa CA Birthday Cupca...  Lisa's Gourmet Snacks Costa Mesa CA  check out...  7144275814

2  Explore Category 'Shorewood IL Birthday Cupcak...  Acapulco Bakery Inc Shorewood, IL - Acapulco B...  8157291737

3  Explore Category 'San Francisco CA Birthday Cu...  Hilda's Mart & Bake Shop San Francisco CA  che...  4153333122

4  Explore Category 'Los Angeles CA Birthday Cupc...  Lenny's Deli Los Angeles, CA - Lenny's Deli ch...  3104755771

5  Explore Category 'San Francisco CA Birthday Cu...  Sweet Inspirations San Francisco CA  check out...        None

6  Explore Category 'Costa Mesa CA Birthday Cupca...  The Cupcake Costa Mesa CA  check out  The Cupc...  9496420571

7  Explore Category 'Los Angeles CA Birthday Cupc...  United Bread & Pastry Inc Los Angeles CA  chec...  3236610037

8  Explore Category 'Garden Grove CA Birthday Cup...  Pescadores Garden Grove CA  check out  Pescado...  7145395585

9  Explore Category 'Bakersfield CA Birthday Cupc...  Bimbo Bakeries Usa Bakersfield CA  check out  ...  6613219352



查看完整回答
反对 回复 2022-06-07
  • 1 回答
  • 0 关注
  • 85 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信