有人知道这个错误吗?显示的错误对我来说没有多大意义,因为我跟踪了他输入的所有内容。是的,该网站是一个用于网络抓取目的的演示网站。
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```
发布于 2021-06-04 20:21:06
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字符串与Python https://blog.finxter.com/python--vs-__r..中的repr。
1如何逐行读取文件并将其存储到。https://blog.finxter.com/how-to-read-a-file-li..。
2如何在Python中将字符串转换为列表?https://blog.finxter.com/how-to-convert-a-stri..。
3如何并行遍历两个列表?https://blog.finxter.com/how-to-iterate-throug..。
4 Python作用域规则- Rules“一个简单的插图。https://blog.finxter.com/python-scoping-rules-..。
5展平Python https://blog.finxter.com/flatten-a-list-of-lis..中的列表列表。
https://stackoverflow.com/questions/67836903
复制相似问题