使用Python爬取腾讯房产的新闻,用的Python库:requests 、re、time、BeautifulSoup ​​​​...

import requests
import re
import time
from bs4 import BeautifulSoup

today = time.strftime('%Y-%m-%d',time.localtime(time.time()))

one_url = 'http://hz.house.qq.com'    #用来构建新的URL的链接

url = 'http://hz.house.qq.com/zxlist/bdxw.htm'      #需要爬取的网址
html = requests.get(url)
html.encoding = html.apparent_encoding
reg = re.compile(r'<a target="_blank" class="tit f-l f16 blue" href="(.*?)">(.*?)</a><span class="tm f-r gray">(.*?)</span>')
html_lis = re.findall(reg,html.text)

for html_li in html_lis:
    new_url = one_url + html_li[0]
    new_time = html_li[2][0:10]             #分割获取到的新闻日期,对比今天的日期和获取到的新闻日期,相同的话就打印出来,不相同就跳过不打印
    if new_time == today:
        print(html_li[1],new_url)
        new_html = requests.get(new_url)            
        soup = BeautifulSoup(new_html.text,'html.parser')
        contents = soup.find_all('p',style="TEXT-INDENT: 2em")
        for content in contents:
            if content.string != None:
                print(content.string)
            else:
                continue
        print('----------------------------下一篇新闻----------------------------')
    else:
        break
#可以建立函数来介绍代码的重复

 

转载于:https://www.cnblogs.com/114811yayi/p/6767883.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值