首页 > 解决方案 > 解析表 - tr.findall('td') - TypeError: 'NoneType' object is not callable

问题描述

有谁知道错误?显示的错误对我来说没有多大意义,因为我遵循了该人输入的所有内容。是的,该网站是一个用于网络抓取目的的演示网站。

from bs4 import BeautifulSoup
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"}


response = requests.get("https://shubhamsayon.github.io/python/demo_html", headers = headers)
webpage = response.content

soup = BeautifulSoup(webpage, "html.parser")

for tr in soup.find_all('tr'):
topic = "TOPIC: "
url = "URL: "
values = [data for data in tr.findall('td')]
for value in values:
print(topic, value.text)
topic = url

C:UsersAndyPycharmProjectspythonProjectvenvScriptspython.exe C:/Users/Andy/PycharmProjects/pythonProject/main.py
Traceback (most recent call last):
File "C:UsersAndyPycharmProjectspythonProjectmain.py", line 14, in
values = [data for data in tr.findall('td')]
TypeError: 'NoneType' object is not callable

Process finished with exit code 1```

标签: pythonweb-scraping

解决方案


from bs4 import BeautifulSoup
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"}


response = requests.get("https://shubhamsayon.github.io/python/demo_html", headers = headers)
webpage = response.content

soup = BeautifulSoup(webpage, "html.parser")

for tr in soup.find_all('tr'):
    topic = "TOPIC: "
    url = "URL: "
    values = [data for data in tr.find_all('td')]
    for value in values:
        print(topic, value.text)
        topic = url

输出:

TOPIC:  __str__ vs __repr__ In Python
URL:  https://blog.finxter.com/python-__str__-vs-__repr__/
....

您也可以尝试使用pandas模块从 url 获取表

    import pandas as pd
    df=pd.read_html("https://shubhamsayon.github.io/python/demo_html")[0]
    df

输出:

```
    TOPIC                                           LINK
0   __str__ vs __repr__ In Python                   https://blog.finxter.com/python-__str__-vs-__r...
1   How to Read a File Line-By-Line and Store Into. https://blog.finxter.com/how-to-read-a-file-li...
2   How To Convert a String To a List In Python?    https://blog.finxter.com/how-to-convert-a-stri...
3   How To Iterate Through Two Lists In Parallel?   https://blog.finxter.com/how-to-iterate-throug...
4   Python Scoping Rules – A Simple Illustrated.   https://blog.finxter.com/python-scoping-rules-...
5   Flatten A List Of Lists In Python               https://blog.finxter.com/flatten-a-list-of-lis...

推荐阅读