抓取新闻数据必须要确定要抓取的新闻目标网站。
优采云 发布时间: 2021-07-08 04:33抓取新闻数据必须要确定要抓取的新闻目标网站。
如果要爬取新闻数据,必须确定要爬取的新闻目标网站。国内新闻网站很多,大大小小的新闻网站上千条。百度是收录近两千条新闻网站。其实我们可以先采集百度新闻。
百度新闻是一个有很多新闻标题和链接的新闻采集页面。我们只需要通过百度新闻提取新闻数据下载
通过这个过程,我们可以制作一个简单的爬虫代码:
使用请求下载百度新闻首页,提取标题,即网页中的链接,然后提取新闻链接,然后下载新闻链接并保存到数据库中。
import requests
import random
# 要访问的目标页面
targetUrl = "http://httpbin.org/ip"
# 要访问的目标HTTPS页面
# targetUrl = "https://httpbin.org/ip"
# 代理服务器(产品官网 www.16yun.cn)
proxyHost = "t.16yun.cn"
proxyPort = "31111"
# 代理验证信息
proxyUser = "username"
proxyPass = "password"
proxyMeta = "http://%(user)s:%(pass)s@%(host)s:%(port)s" % {
"host" : proxyHost,
"port" : proxyPort,
"user" : proxyUser,
"pass" : proxyPass,
}
# 设置 http和https访问都是用HTTP代理
proxies = {
"http" : proxyMeta,
"https" : proxyMeta,
}
# 设置IP切换头
tunnel = random.randint(1,10000)
headers = {"Proxy-Tunnel": str(tunnel)}
resp = requests.get(targetUrl, proxies=proxies, headers=headers)
print resp.status_code
print resp.text