import requests
from bs4 import BeautifulSoup
import cloudscraper
from pathlib import Path
def fiveLetterWordScrapper():
pageNum = 1
while pageNum != 51:
scraper = cloudscraper.CloudScraper()
page = scraper.get('https://www.thewordfinder.com/wordlist/5-letter-words/?dir=ascending&field=word&pg=' + str(pageNum) + '&size=5')
soup = BeautifulSoup(page.content, 'html.parser')
findWords = soup.find('ul', class_ = 'clearfix')
arrWordList = [x.get_text().strip()[0:5] for x in findWords if any(x.get_text().strip())]# the line that gives the error
with open(Path.cwd()/ 'fiveWordList.txt', 'a') as wordList:
for word in arrWordList:
wordList.write(word.lower() + '\n')
pageNum += 1
fiveLetterWordScrapper()
我有一个脚本,它会浏览一个网站,实际上找到所有存在的 5 个字母的单词,并将其放入一个 txt 文件中。大约两周前,当我运行该脚本时,它运行得很好,并且按预期运行,但现在当我尝试运行该程序时,它返回“TypeError:'NoneType'对象不可迭代。”
我认为我从代码第一次运行时起就没有对代码进行任何更改,所以我不明白为什么现在当我尝试运行它时会出现错误。
它对我来说效果很好,这是我的 .txt 文件的输出。
aahed
aalii
aargh
abaca
abaci
aback
abacs
abaft
....
如果您收到非类型错误,则您的计算机可能被服务器阻止。
尝试使用网络浏览器手动访问该页面,看看是否遇到任何验证码。您还可以检查脚本中的响应状态代码 (page.status_code),看看它是否返回任何错误。