python之爬虫demo

import urllib.request,re

def download_html(url):
    header = {
        "User-Agent": "Mozilla/5.0(Windows NT 10.0;WOW64"
                      "AppleWebKit/53.36(KHTML,like Gecko)"
                      "Chrome/76.0.3809.87 Safari/537.36 SLBrowser/6.0.1.8131"
    }
    req=urllib.request.Request(url=url,headers=header)

    response=urllib.request.urlopen(req)

    html=response.read().decode("utf-8")

    return html

def extract_url(html):
    pattern = "https://movie.douban.com/subject/[0-9]+/"
    # 从html中提取全部满足电影介绍页网址pattern的url
    urls = re.findall(pattern, html)
    # 去除页面当中重复的url
    return  set(urls)

file=open('H://WorkPlace//PyWorkPlace//test1//douban.txt','r')

output=open('H://WorkPlace//PyWorkPlace//test1//movie.txt','w')

#
lines=file.readlines()

for url in lines:
    #遍历每一行url,通过strip函数去掉收尾空字符
    url=url.strip()
    print(url)

    html=download_html(url)
    urls=extract_url(html)

    for url in urls:
        output.write(url+'\n')
#关闭俩个文件
file.close()
output.close()


测试效果展示: 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值