Adblock阻断剂阻断urllib.request.urlopen()

2024-04-26 09:38:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Python来抓取报纸站点,并在删除各种HTML标记后以文本形式收集真实的故事

我的简单代码如下

import urllib.request
from bs4 import BeautifulSoup

#targetURL = 'http://indianexpress.com/article/india/mamata-banerjee-army-deployment-nh-2-in-west-bengal-military-coup-4405871'
targetURL = "http://timesofindia.indiatimes.com/india/Congress-Twitter-hacking-Police-form-cyber-team-launch-probe/articleshow/55737598.cms"
#targetURL = 'http://www.telegraphindia.com/1161201/jsp/nation/story_122343.jsp#.WEDzfXV948o'

with urllib.request.urlopen(targetURL) as url:
    html = url.read()
soup = BeautifulSoup(html,'lxml')

for el in soup.find_all("p"):
    print (el.text)

当我访问印度快讯网URL或telegraphindia.com公司网址,代码工作得很好,我得到的故事,大体上没有垃圾,纯文本形式。在

但是timesofindia.com站点有一个adblock拦截器,在这种情况下,输出如下:

^{pr2}$

如何绕过这个Adblock拦截器并检索页面?如有任何建议,将不胜感激


Tags: 代码in文本importcomhttp站点request
1条回答
网友
1楼 · 发布于 2024-04-26 09:38:43

看起来您要提取的实际内容不在<p>标记内。然而,广告拦截器警告就在这样的标签内。此文本始终是HTML文档的一部分,但只有当广告无法加载时,用户才能看到此文本。在

请尝试提取<arttextxml>标记的内容。在

相关问题 更多 >