python数据收集系统_玩玩Python数据采集_001
from urllib.request import urlopen
from urllib.error import HTTPError, URLError
from bs4 import BeautifulSoup
import time
def get_html():
try:
html = urlopen("http://news.sina.com.cn/hotnews/")
except (HTTPError, URLError) as e: # 如果遇到 http exceptions 或者 url exceptions return -> None
return None
try:
bsObj = BeautifulSoup(html) # t=bsObj.find_all(text="一起了解 .Net Foundation 項目 No.16")
news_list=bsObj.find_all(target="_blank") # 指定查詢具體對應的新聞咨詢標簽屬性值
for new in news_list:
new_title=new.get_text() # 獲取新聞資訊的Title
new_url=new["href"] # 獲取新聞資訊的Url
if new_url != "": # 去除空連接的新聞資訊
res = (time.strftime("%Y-%m-%d") + "標題為: {},如需閱讀請點擊該鏈接: {}").format(new_title,new_url) #加個時間,點綴一下
f=open('news_log','a',encoding="UTF-8")
f.write(res) # 寫入新聞資訊
f.write("\n")
f.close() # 關閉文件
else:
pass
except AttributeError as e: # 如果在當前頁面找不到對應標簽或者屬性值 return -> None
return None
get_html()
總結
以上是生活随笔為你收集整理的python数据收集系统_玩玩Python数据采集_001的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 键盘连击测试_测试梗欢迎补充
- 下一篇: python中json模块博客园_Pyt