[英]Solve TypeError: 'NoneType' object is not callable in webscraper
所以我正在尝试创建一个转到网站的 Python 脚本。 查看页面上所有硬币的结果。 点击所有结果,查看页面是否有电报链接,如果有则复制并添加到打印结果中,如果没有则只打印币名。 我在第 23 行的 element.click() 中收到 TypeError: 'NoneType' object is not callable 错误。 我不确定如何解决它。
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
# Set up a webdriver to simulate a browser
driver = webdriver.Chrome()
# Send a request to the webpage
response = requests.get('https://www.coingecko.com/en/new-cryptocurrencies')
# Parse the response
soup = BeautifulSoup(response.text, 'html.parser')
# Find all elements with the class "coin-name"
coin_elements = soup.find_all(class_='coin-name')
# Iterate through the coin elements
for element in coin_elements:
# Extract the text from the element (the coin name)
coin_name = element.text
# Simulate a click on the element
element.click()
# Wait for the page to load
time.sleep(2)
# Parse the content of the coin's page
coin_soup = BeautifulSoup(driver.page_source, 'html.parser')
# Find the element containing the Telegram group link
telegram_element = coin_soup.find('a', href=re.compile(r'^https://t\.me/'))
# Check if the element was found
if telegram_element is not None:
# Extract the link from the element
telegram_link = telegram_element['href']
# Print the coin name and link
print(f'{coin_name}: {telegram_link}')
else:
# Print a message if the element was not found
print(f'{coin_name}: No Telegram group found')
# Close the webdriver
driver.close()
错误:
line 23, in <module>
element.click()
TypeError: 'NoneType' object is not callable
你不能混淆 selenium 和 BeautifulSoup。 此coin_elements = soup.find_all(class_='coin-name')
不会为您提供WebElement
的列表,因此您无法对其执行click()
。
查看此线程以检查 BS 中的find_all
返回的类型: What is the return type of the find_all method in Beautiful Soup?
使用find_elements_by_xpath
方法选择可点击的元素。 Selenium 不接受其他数据类型的click
操作。
你可以尝试类似的东西,
elements = driver.find_elements_by_xpath('//*[@class='coin-name']')
for element in elements:
element.click()
注意:从你的回溯看来你有NoneType
,这可能是检查选择器一次的指标。
您根本不需要使用硒。 使用 python requests 和 bs4 你可以做到这一点。
首先获取硬币的所有 url,然后对其进行迭代并从页面获取响应并获取电报链接。
代码:
import requests
from bs4 import BeautifulSoup
import requests
import re
response = requests.get('https://www.coingecko.com/en/new-cryptocurrencies')
# Parse the response
soup = BeautifulSoup(response.text, 'html.parser')
coin_elements =["https://www.coingecko.com" + item['href'] for item in soup.select("table.sort a[href*='en/coins']")]
#print(coin_elements)
coin_elements_text =[item.text.strip() for item in soup.select("table.sort a[href*='en/coins']")]
for pageurl,coin_name in zip(coin_elements,coin_elements_text):
r=requests.get(pageurl)
coin_soup=BeautifulSoup(r.text, 'html.parser')
telegram_element = coin_soup.select_one("a[rel='nofollow noopener'][href*='t.me']")
try:
# Extract the link from the element
telegram_link = telegram_element['href']
# Print the coin name and link
print(f'{coin_name}: {telegram_link}')
except:
# Print a message if the element was not found
print(f'{coin_name}: No Telegram group found')
控制台输出:
FinToken
FTC: https://t.me/FintokenEN
Canadian Inuit Dog
CADINU: https://t.me/cadinu
DEXO
DEXO: https://t.me/DexoOfficial
DexWallet
DWT: https://t.me/Dexwalletofficialcommunity
Saracens Fan Token
SARRIES: https://t.me/ChilizTrade
Tsuki no usagi
GYOKUTO: https://t.me/GyokutoEntry
Falcon
FLN: https://t.me/SpaceDexPortal
Dejitaru Shirudo
SHIELD: https://t.me/dejitarushirudoentry
Cronos ID
CROID: No Telegram group found
Chikn Worm
WORM: No Telegram group found
Huckleberry Inu
HKBY: https://t.me/huckleberryinuofficial
Wrapped OAS
WOAS: No Telegram group found
Dogu Inu
DOGU: https://t.me/Dogu_Inu
Harlequins Fan Token
QUINS: https://t.me/ChilizTrade
Sonic Suite
SONIC: https://t.me/sonicsuiteeth
Matchcup
MATCH: https://t.me/Matchcupofficial
Mu Inu
MUINU: No Telegram group found
IMPT
IMPT: https://t.me/IMPTprogram
Radioreum
THERADIO: https://t.me/radioreum
Centurion Invest
CIX: https://t.me/centurioninvestgroup
Liquid ASTR
NASTR: https://t.me/Algem_io
Rabbit2023
...so on
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.