使用Python爬取腾讯房产的新闻,用的Python库:requests 、re、time、BeautifulSoup ????,,import req
使用Python爬取腾讯房产的新闻,用的Python库:requests 、re、time、BeautifulSoup ????,,import req
import requestsimport reimport timefrom bs4 import BeautifulSouptoday = 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_encodingreg = 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#可以建立函数来介绍代码的重复
使用Python爬取腾讯房产的新闻,用的Python库:requests 、re、time、BeautifulSoup ????
相关内容
- Python程序中的进程操作-进程同步(multiprocess.Lock),,
- [Python]sort与sorted高级技巧,,与其他语言不同,py
- 定义一个方法get_page(url),url参数是需要获取网页内容的
- python基础学习11(核心编程第二版)部分,,#-*-coding
- Python贪吃蛇,,#基于pygame设
- python3学习,有c++的基础,,记事本编辑,个人学习
- python关系运算符or、and注意点,,or运算符运算符从左
- Python-百度经纬度转高德经纬度,,import mat
- python字典顺序转字符串,,普通字典默认是无序的
- Python基础(二)之数据类型和运算(1)——数字,,数
评论关闭