我需要抓取一个网页(https://www304.americanexpress.com/credit-card/compare),但我遇到了一个问题--我需要在首页显示的文本绝对隐藏在许多不同的格式化标记中。
我知道如何使用Beautiful Soup抓取常规页面,但这并不能满足我的需要(例如,文本丢失,一些标签通过...)
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 (''.join([element.text for element in soup.body.find_all(lambda tag: tag != 'script', recursive=False)]))有没有一种特殊的方法来抓取这个特殊的网页?
发布于 2014-09-09 23:49:37
这只是一个普通的网页。例如,<span class="card-offer-des">包含文本after you use your new Card to make $1,000 in purchases within the first 3 months.。我还试着在浏览器中关闭Javascript。文本仍然存在,就像它应该存在的那样。
所以我真的看不出有什么问题。另外,我建议您尝试学习lxml和xpath。一旦你知道了它是如何工作的,实际上就更容易得到你想要的文本了。
发布于 2014-09-09 23:58:39
您应该尝试使用python的代码是:
if not "what-have-you" in StringPulledFromSite: continue;
if "what-have-you" in StringPulledFromSite:
[your code to save to the filesystem];你应该瞄准的字符串应该是这样的:
((<span class=\") && (/>))您应该尝试找到这两个(并尝试具体,以便您可以很容易地与它们区分)。找到后,保存字符串,测试并保存文本。
https://stackoverflow.com/questions/25748857
复制相似问题