【问题标题】:Webscraper bugging out from additional value网络爬虫从附加价值中脱颖而出
【发布时间】:2021-07-29 12:17:58
【问题描述】:

所以我已经完成了我的第一个网络爬虫,一切正常,除了一件事,我不知道为什么。我第一次使用代码for x in range(1,6): getQuestions('bygg', x) 抓取工作正常,但后来我添加了for x in range(1,6): getQuestions('bygg', x) getQuestions('advokat', x),它只返回0(TypeError:'NoneType' 对象不可下标),问题似乎来自我的 'nummer': item.find('a', {'class': 'link-body'})['href'],,因为它说'nummer': item.find('a', {'class': 'link-body'})['href'], TypeError: 'NoneType' object is not subscriptable

这是完整的代码

    import requests
from bs4 import BeautifulSoup
import pandas as pd 

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36'}

questionlist = []

def getQuestions(tag, page):
    url = f'https://www.merinfo.se/search?d=c&ap=1&emp=0%3A20&rev=0%3A100&who={tag}&bf=1&page={page}'
    r = requests.get(url, headers=headers)
    soup = BeautifulSoup(r.text, 'html.parser')
    questions = soup.find_all('div', {'class': 'box-white p-0 mb-4'})
    for item in questions:
        question = {
        'tag': tag,
        'title': item.find('a', {'class': 'link-primary'}).text,
        'link': item.find('a', {'class': 'link-primary'})['href'],
        'nummer': item.find('a', {'class': 'link-body'})['href'],
        'address': item.find('address', {'class': 'mt-2 mb-0'}).text,
        'RegÅr': item.find('div', {'class': 'col text-center'}).text,
        }
        questionlist.append(question)
    return

for x in range(1,6):
    getQuestions('bygg', x)
    getQuestions('advokat', x)

df = pd.DataFrame(questionlist)
df.to_excel('merinfo skrapare för bygg.xlsx')
print('LBC Marketing TM')

最后一点,如果我#out 'nummer': item.find('a', {'class': 'link-body'})['href'], 它工作正常,但这是最重要的部分哈哈。

感谢您的帮助,最好的问候!

【问题讨论】:

  • 请编辑您的问题并给它一个有意义的标题。问题应该是独立的。如果不是,请说明如何。
  • 问题是 item.find('a', {'class': 'link-body'}) 正在返回 None

标签: python pandas web-scraping


【解决方案1】:

正如@AndyKnight 提到的,您正在尝试访问 ['href'] 对一个无项目。您可以为 None 添加一些健全性检查以提供帮助。比如:

def get_href_item(src_item, tag, class_name):
    href_item = src_item.find(tag, {"class": f"{class_name}"})
    if href_item is not None:
        href = href_item['href']
        if href is not None:
            return href
        else:
            return "HREF_NOT_FOUND"

然后您可以使用该方法获取“nummer”值:

question = {
        'tag': tag,
        'title': item.find('a', {'class': 'link-primary'}).text,
        'link': item.find('a', {'class': 'link-primary'})['href'],
        'nummer': get_href_item(item, 'a', 'link-body'),
        'address': item.find('address', {'class': 'mt-2 mb-0'}).text,
        'RegÅr': item.find('div', {'class': 'col text-center'}).text,
        }

您可能希望为您正在搜索的所有值添加类似的“无”完整性检查。

【讨论】:

  • 问题是href不能是None,因为当我搜索Bygg而不是Advokat时,它工作正常并且它具有相同的HTML代码,这是令人困惑的部分。
  • @CavTGpunk 我修改了您的代码,正如我在上面的答案中概述的那样,它运行时没有遇到TypeError: 'NoneType' object is not subscriptable 错误。按原样运行您的代码,并发生错误。
  • 非常感谢您的帮助!我刚刚弄清楚如何实现你给我的代码 {lol} 并且它有效!你太棒了。
猜你喜欢
  • 1970-01-01
  • 2011-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-08
相关资源
最近更新 更多