【问题标题】:Simple web crawler in Python3 not producing output. What's going on?Python3 中的简单网络爬虫不产生输出。这是怎么回事?
【发布时间】:2018-12-09 20:34:39
【问题描述】:

我在运行此处列出的示例代码时遇到问题:https://dev.to/pranay749254/build-a-simple-python-web-crawler

这是我正在尝试运行的代码:

import requests
from bs4 import BeautifulSoup
def web(page,WebUrl):
    if(page>0):
        url = WebUrl
        code = requests.get(url)
        plain = code.text
        s = BeautifulSoup(plain, "html.parser")
        for link in s.findAll('a', {'class':'s-access-detail-page'}):
            tet = link.get('title')
            print(tet)
            tet_2 = link.get('href')
            print(tet_2)
web(1,'https://www.amazon.com/s/ref=nb_sb_noss_2?url=search-alias%3Daps&field-keywords=Stomp+box&rh=i%3Aaps%2Ck%3AStomp+box')

当我通过终端 shell(我正在运行 Ubuntu 16.04)运行上述代码时,程序不返回任何输出。我使用以下命令:

$ python3 BasicCrawler.py

我按下回车键,终端上没有打印任何文本,就好像我没有运行任何脚本一样。我可以在程序中添加 print() 命令来验证它是否正在运行,但它似乎没有打印变量 tet 或 tet2。

【问题讨论】:

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


    【解决方案1】:

    我进入 shell_plus 并运行了你的代码并打印了几张照片:

    In [9]: import requests
       ...: from bs4 import BeautifulSoup
       ...: def web(page,WebUrl):
       ...:     if(page>0):
       ...:         print('page is greater than zero')
       ...:         url = WebUrl
       ...:         code = requests.get(url)
       ...:         plain = code.text
       ...:         s = BeautifulSoup(plain, "html.parser")
       ...:         # hmm, no results
       ...:         #for link in s.findAll('a', {'class':'s-access-detail-page'}):
       ...:         # lets try more open query path to see if we get hits
       ...:         for link in s.findAll('a'):
       ...:             print('link: ', link)
       ...:             tet = link.get('title')
       ...:             print(tet)
       ...:             tet_2 = link.get('href')
       ...:             print(tet_2)
       ...: web(1,'https://www.amazon.com/s/ref=nb_sb_noss_2?url=search-alias%3Daps&
       ...: field-keywords=Stomp+box&rh=i%3Aaps%2Ck%3AStomp+box')
    

    我发现原来的代码没有结果,所以没有输出。然后,我将输入更改为 findAll 只是找到一个标签并得到结果,所以我猜测目标页面此时实际上没有任何带有“s-access-detail-page”类的标签?

    【讨论】:

    • 那个 URL 返回状态码 503;事实上,其中没有包含该类的“a”标签。你比我在测试时快。我建议在尝试 for 循环之前检查 URL 的 status_code。 IF code.status_code == 200:
    【解决方案2】:

    您需要传递一个 User-Agent 标头。此外,对于我使用的方法,您不能在 CSS 选择器中使用复合类名称,因此必须将空格替换为“。”。我也完成了相关链接。

    在您的代码中,您需要以下内容:

    code = requests.get(url,  headers = {'User-agent': 'Mozilla/5.0'})
    

    我的版本:

    import requests
    from bs4 import BeautifulSoup
    
    re = requests.get('https://www.amazon.com/s/ref=nb_sb_noss_2?url=search-alias%3Daps&field-keywords=Stomp+box&rh=i%3Aaps%2Ck%3AStomp+box', headers = {'User-agent': 'Mozilla/5.0'} )
    print(re.status_code)
    soup = BeautifulSoup(re.content, 'lxml')
    base = 'https://www.amazon.com'
    links = [(link['title'] ,link['href']) for link in soup.select("a.a-link-normal.s-access-detail-page.s-color-twister-title-link.a-text-normal" )]
    links = [(link[0], base + link[1])  if 'slredirect' in link[1] else link for link in links]
    print(links)
    

    【讨论】:

      【解决方案3】:

      它没有打印任何东西,因为列表可能是空的。 这意味着类名“s-access-detail-page”的“a”标签在您正在解析的 html 文档中不包含任何内容。

      打印 HTTP 响应的输出并尝试找出要在哪个块上迭代和获取数据的确切类。

      【讨论】:

        猜你喜欢
        • 2023-03-13
        • 1970-01-01
        • 2017-01-26
        • 2016-06-23
        • 1970-01-01
        • 2021-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多