【发布时间】:2014-10-26 06:39:24
【问题描述】:
我的问题是我正在尝试抓取一堆不同的网站以查找所有可见文本以下载到 .txt 文件 - 不幸的是,我无法从这些网站获取所有可能的文本。我在下面发布了我的代码的工作示例:
import requests
from bs4 import BeautifulSoup
from collections import Counter
urls = ['https://www304.americanexpress.com/credit-card/compare']
with open('thisisanew.txt', 'w', encoding='utf-8') as outfile:
for url in urls:
website = requests.get(url)
soup = BeautifulSoup(website.content)
text = [''.join(s.findAll(text=True))for s in soup.findAll('p')]
for item in text:
print(item, file=outfile)
如果你测试这段代码,你得到的只是以下数据——
Ratings & Reviews for this card are currently not available
Ratings & Reviews for this card are currently not available
Ratings & Reviews for this card are currently not available
All users of our online services subject to Privacy Statement and agree to be bound by Terms of etc...
我究竟如何获得此页面上的其余可见数据?根据我的研究,我很确定这与我的 soup.findAll('p')] 参数有关,但我不知道要添加什么来获取其余数据。
【问题讨论】:
标签: python python-3.x html-parsing beautifulsoup