美文网首页
python-爬虫案例7-爬取网页中所有涉及的内容

python-爬虫案例7-爬取网页中所有涉及的内容

作者: 我最有才 | 来源:发表于2019-04-03 10:34 被阅读0次

模拟 https://news.sina.com.cn/ 并爬取网页上几乎所有连接的网页内容   2019.0403

网页软代码如下: 并找到我们要的内容--红色标识

代码如下:

结果如下:

打开后如下:

可复制代码如下:

import re

import urllib.request

data=urllib.request.urlopen("http://news.sina.com.cn/").read()

data2=data.decode("utf-8","ignore")

pat='href="(https://news.sina.com.cn/.*?)"'

allurl=re.compile(pat).findall(data2)

for i in range(0,len(allurl)):

    try:

        print("第"+str(i)+"次爬取")

        thisurl=allurl[i]

        file="E:/py/sinanews/"+str(i)+".html"

        urllib.request.urlretrieve(thisurl,file)

        print("----成功----")

    except urllib.error.URLError as e:

        if hasattr(e,"code"):

            print(e.code)

        if hasattr(e,"reason"):

            print(e.reason)

相关文章

网友评论

      本文标题:python-爬虫案例7-爬取网页中所有涉及的内容

      本文链接:https://www.haomeiwen.com/subject/xffobqtx.html