【问题标题】:Web Crawler keeps saying no attribute even though it really has网络爬虫一直说没有属性,即使它确实有
【发布时间】:2016-12-26 05:09:28
【问题描述】:

我一直在为这个网站 (http://www.bobaedream.co.kr/cyber/CyberCar.php?gubun=I&page=1) 开发一个网络爬虫。但是我在抓取股票的每个标题时遇到了麻烦。我很确定 carinfo_title = carinfo.find_all('a', class_='title') 有属性。

请查看附件代码和网站代码,然后给我任何建议。

谢谢。

(网站代码)

https://drive.google.com/open?id=0BxKswko3bYpuRV9seTZZT3REak0

(我的代码)

from bs4 import BeautifulSoup
import urllib.request

target_url = "http://www.bobaedream.co.kr/cyber/CyberCar.php?gubun=I&page=1"

def fetch_post_list():
    URL = target_url
    res = urllib.request.urlopen(URL)
    html = res.read()
    soup = BeautifulSoup(html, 'html.parser')
    table = soup.find('table', class_='cyber')

    #Car Info and Link
    carinfo = table.find_all('td', class_='carinfo')
    carinfo_title = carinfo.find_all('a', class_='title')

    print (carinfo_title)

    return carinfo_title

fetch_post_list()

【问题讨论】:

    标签: python-3.x attributes beautifulsoup web-crawler


    【解决方案1】:

    您有 多个元素carinfo 类,并且对于每个“carinfo”,您都需要获取汽车标题。循环table.find_all('td', class_='carinfo')的结果:

    for carinfo in table.find_all('td', class_='carinfo'):
        carinfo_title = carinfo.find('a', class_='title')
        print(carinfo_title.get_text())
    

    将打印:

    미니 쿠퍼 S JCW
    지프 랭글러 3.8 애니버서리 70주년 에디션
    ...
    벤츠 뉴 SLK200 블루이피션시
    포르쉐 뉴 카이엔 4.8 GTS
    마쯔다 MPV 2.3
    

    请注意,如果您只需要汽车标题,可以将其简化为一行:

    print([elm.get_text() for elm in soup.select('table.cyber td.carinfo a.title')])
    

    .select() 方法中的字符串是CSS selector

    【讨论】:

    • 谢谢我知道了!!感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-28
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多