【问题标题】:BeautifulSoup Scraping FormattingBeautifulSoup 抓取格式
【发布时间】:2018-12-28 07:37:28
【问题描述】:

这是我第一次使用BeautifulSoup,我正试图从当地便利店中删除商店位置数据。

但是,当数据被传递到 CSV 文件时,我在尝试删除空行时遇到了一些问题,我试过 .replace('\n','').strip() 都没有工作。

此外,我在拆分被抓取并包含在同一个兄弟方法中的数据时遇到问题。

我在下面添加了脚本:

from bs4 import BeautifulSoup
from requests import get
import urllib.request
import sched, time
import csv

url = 'http://www.cheers.com.sg/web/store_location.jsp'

response = get(url)
soup = BeautifulSoup(response.text, 'html.parser')
#print (soup.prettify())

#open a file for writing
location_data = open('data/soupdata.csv', 'w', newline='')

#create the csv writer object
csvwriter = csv.writer(location_data)

cheers = soup.find('div' , id="store_container")

count = 0

#Loop for Header tags
for paragraph in cheers.find_all('b'):

    header1 = paragraph.text.replace(':' , '')
    header2 = paragraph.find_next('b').text.replace(':' , '')
    header3 = paragraph.find_next_siblings('b')[1].text.replace(':' , '')
    if count == 0:      
        csvwriter.writerow([header1, header2, header3])
        count += 1

    break

for paragraph in cheers.find_all('br'): 
    brnext = paragraph.next_sibling.strip()
    brnext1 = paragraph.next_sibling
    test1 = brnext1.next_sibling.next_sibling

    print(test1)
    csvwriter.writerow([brnext, test1])

location_data.close()

生成的输出示例:

输出应该是什么样子的示例:

我怎样才能做到这一点?

提前致谢。

【问题讨论】:

    标签: python csv web-scraping beautifulsoup


    【解决方案1】:

    你只需要改变提取地址、电话和营业时间的方式

    import csv
    
    from bs4 import BeautifulSoup
    from requests import get
    
    url = 'http://www.cheers.com.sg/web/store_location.jsp'
    
    response = get(url)
    soup = BeautifulSoup(response.text, 'html.parser')
    # print (soup.prettify())
    
    # open a file for writing
    location_data = open('data/soupdata.csv', 'w', newline='')
    
    # create the csv writer object
    csvwriter = csv.writer(location_data)
    
    cheers = soup.find('div', id="store_container")
    
    count = 0
    
    # Loop for Header tags
    for paragraph in cheers.find_all('b'):
        header1 = paragraph.text.replace(':', '')
        header2 = paragraph.find_next('b').text.replace(':', '')
        header3 = paragraph.find_next_siblings('b')[1].text.replace(':', '')
        if count == 0:
            csvwriter.writerow([header1, header2, header3])
            count += 1
    
        break
    
    for paragraph in cheers.find_all('div'):
        label = paragraph.find_all('b')
        if len(label) == 3:
            print(label)
            address = label[0].next_sibling.next_sibling
            tel = label[1].next_sibling
            hours = label[2].next_sibling
            csvwriter.writerow([address, tel, hours])
    
    location_data.close()
    

    【讨论】:

    • 这完美的作品被困在这几个小时。感谢您的帮助
    【解决方案2】:

    为了使它稍微有条理,你可以尝试如下。我使用了.select() 而不是.find_all()

    import csv
    from bs4 import BeautifulSoup
    import requests
    
    url = 'http://www.cheers.com.sg/web/store_location.jsp'
    
    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'html.parser')
    with open("output.csv","w",newline="") as infile:
        writer = csv.writer(infile)
        writer.writerow(["Address","Telephone","Store hours"])
    
        for items in soup.select("#store_container .store_col"): 
            addr = items.select_one("b").next_sibling.next_sibling
            tel = items.select_one("b:nth-of-type(2)").next_sibling
            store = items.select_one("b:nth-of-type(3)").next_sibling
            writer.writerow([addr,tel,store])
    

    【讨论】:

    • 也感谢您的帮助,不胜感激。也只是想知道,有没有一种方法可以创建一个脚本,可以自动抓取具有不同结构的多个站点?为单个站点创建单独的脚本会很痛苦。
    • 当 html 结构不同时,您的脚本也应该不同,否则为一个站点创建的脚本在应用于其他站点时会惨遭失败。不过,还是有一些希望的。看看这个Natural language processing
    • 数字尽可能多地认为需要人工智能进行某种处理才能真正进行自主抓取。哈哈。只是我需要一段时间来学习如何在其他网站上使用 beautifulsoup。在这个平台再问太麻烦了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 2021-01-04
    • 1970-01-01
    • 2012-10-31
    相关资源
    最近更新 更多