【问题标题】:<br> Tag parsing using python and beautifulsoup<br> 标签解析使用python和beautifulsoup
【发布时间】:2015-07-09 20:48:19
【问题描述】:

因此,我正在尝试从给定网站中提取高尔夫球场数据,在该网站中,它将创建一个包含名称和地址的 CSV。对于地址,虽然我从中获取数据的网站有
标记将其分开。是否可以将由
分隔的两个地址解析为两个单独的列?

所以它在 HTML 上看起来像这样

<div class="location">10799 E 550 S<br>Zionsville, Indiana, United States</div>

我希望它会被分解成

Column1:10799 E 550 S
Column2:Zionsville, Indiana, United States

这是我的代码:

import csv
import requests
from bs4 import BeautifulSoup

courses_list = []

with open('Garmin_GC.csv', 'w') as file:
    writer = csv.writer(file)
    for i in range(3):  #893
        url = "http://sites.garmin.com/clsearch/courses/search?course=&location=&country=US&state=&holes=&radius=&lang=en&search_submitted=1&per_page={}".format(
            i * 20)
        r = requests.get(url)
        soup = BeautifulSoup(r.text)
        g_data2 = soup.find_all("div", {"class": "result"})
        for item in g_data2:
            try:
                name = item.find_all("div", {"class": "name"})[0].text
            except IndexError:
                name = ''
                print "No Name found!"
            try:    
                address = item.find_all("div", {"class": "location"})[0].get_text(separator=' ')
                print address
            except IndexError:
                address = ''
                print "No Address found!"
            writer.writerow([name.encode("utf-8"), address.encode("utf-8")])

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:

    使用.stripped_strings generator

    address = list(item.find('div', class_='location').stripped_strings)
    

    这将产生一个包含两个字符串的列表:

    >>> from bs4 import BeautifulSoup
    >>> markup = '''<div class="location">10799 E 550 S<br>Zionsville, Indiana, United States</div>'''
    >>> soup = BeautifulSoup(markup)
    >>> list(soup.find('div', class_='location').stripped_strings)
    [u'10799 E 550 S', u'Zionsville, Indiana, United States']
    

    把它放在你的代码上下文中:

    try:
        name = item.find('div', class_='name').text
    except AttributeError:
        name = u''
    try:
        address = list(item.find('div', class_='location').stripped_strings)
    except AttributeError:
        address = [u'', u'']
    writer.writerow([v.encode("utf-8") for v in [name] + address])
    

    将两个地址值写入两个单独的列。

    【讨论】:

    • AttributeError: 'list' object has no attribute 'encode' 我收到此错误。你能帮我解决这个问题吗
    • @Gonzalo68:你现在有一个包含 unicode 字符串的列表。编码列表的内容,而不是列表本身。
    • 我该怎么做?
    • 我收到无效的语法错误。 :(
    • @Gonzalo68:是的,] 右括号丢失,现在添加。
    猜你喜欢
    • 2017-09-24
    • 1970-01-01
    • 2016-02-14
    • 2011-01-19
    • 2012-05-22
    • 1970-01-01
    • 2013-03-20
    • 2019-05-19
    • 1970-01-01
    相关资源
    最近更新 更多