【问题标题】:Getting a not subscriptable when running a web scraping script运行网络抓取脚本时获取不可下标
【发布时间】:2022-01-31 14:47:04
【问题描述】:

我正在练习网页抓取并使用此代码。我正在尝试 for 循环。

import requests
from bs4 import BeautifulSoup

name=[]
link=[]
address=[]
for i in range (1,11):
  i=str(i)
  url = "https://forum.iktva.sa/exhibitors-list?&page="+i+"&searchgroup=37D5A2A4-exhibitors"
  soup = BeautifulSoup(requests.get(url).content, "html.parser")

  for a in soup.select(".m-exhibitors-list__items__item__header__title__link"):
      company_url = "https://forum.iktva.sa/" + a["href"].split("'")[1]

      soup2 = BeautifulSoup(requests.get(company_url).content, "html.parser")
      n=soup2.select_one(".m-exhibitor-entry__item__header__title").text

      l=soup2.select_one("h4+a")["href"]
      a=soup2.select_one(".m-exhibitor-entry__item__body__contacts__address").text
      name.append(n)
      link.append(l)
      address.append(a)

当我运行程序时出现此错误:

  l=soup2.select_one("h4+a")["href"]
TypeError: 'NoneType' object is not subscriptable

如果我不确定如何解决问题。

【问题讨论】:

    标签: python ajax web-scraping beautifulsoup python-requests


    【解决方案1】:

    你只需要 raplace,按照代码 Handle None

    l = soup2.select_one("h4+a")
    if l:
        l = l["href"]
    else:
        l = "Website not available"
    

    如您所见,因为网站不适用于: https://forum.iktva.sa/exhibitors/sanad

    或者您可以处理所有错误,例如:

    import requests
    from bs4 import BeautifulSoup
    
    
    def get_object(obj, attr=None):
        try:
            if attr:
                return obj[attr]
            else:
                return obj.text
        except:
            return "Not available"
    
    
    name = []
    link = []
    address = []
    for i in range(1, 11):
        i = str(i)
        url = f"https://forum.iktva.sa/exhibitors-list?&page={i}&searchgroup=37D5A2A4-exhibitors"
        soup = BeautifulSoup(requests.get(url).text, features="lxml")
    
        for a in soup.select(".m-exhibitors-list__items__item__header__title__link"):
    
            company_url = "https://forum.iktva.sa/" + a["href"].split("'")[1]
            soup2 = BeautifulSoup(requests.get(company_url).content, "html.parser")
    
            n = soup2.select_one(".m-exhibitor-entry__item__header__title").text
            n = get_object(n)
    
            l = soup2.select_one("h4+a")
            l = get_object(l, 'href')
    
            a = soup2.select_one(".m-exhibitor-entry__item__body__contacts__address")
            a = get_object(a)
    
            name.append(n)
            link.append(l)
            address.append(a)
    

    【讨论】:

    • 使用此代码时出现此错误:文件“main.py”,第 30 行,在 汤 = BeautifulSoup(requests.get(url).text, features="lxml" )文件“/opt/virtualenvs/python3/lib/python3.8/site-packages/bs4/__init__.py”,第 245 行,在 init 中引发 FeatureNotFound(bs4.FeatureNotFound:找不到具有您要求的功能的树生成器:lxml。您需要安装解析器库吗?
    • 您可以使用 html.parser 但我使用了 lxml(它比 html 解析器更快)。 (pip install lxml)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-21
    • 2021-07-22
    • 2021-11-20
    相关资源
    最近更新 更多