日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

行业大数据爬虫练习

發(fā)布時(shí)間:2023/12/10 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 行业大数据爬虫练习 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1、使用正則表達(dá)式查找使用requests庫(kù)獲取的網(wǎng)頁(yè)https://www.cnki.net/ 內(nèi)容中的title內(nèi)容;

import requests import re r = requests.get('https://www.cnki.net/') r.encoding = 'utf-8' # print(r.text) titles = re.findall(r"<title.*?>(.+?)</title>", r.text) print(titles)

2、使用Xpath和Beautiful Soup查找網(wǎng)站https://weixin.sogou.com/中的熱詞,并將其存儲(chǔ)為Jason格式文檔。

#(1)Xpath方法 import requests from lxml import etree from json import dumps import lxml url="https://weixin.sogou.com/" html=requests.get(url) html.encoding="utf-8" selecter=etree.HTML(html.text)dict = {1:'',2:'',3:'',4:'',5:'',6:'',7:'',8:'',9:'',10:'',} for i in range(1,11):ppath = """//*[@id="topwords"]/li["""+str(i)+"""]/a/text()"""s=selecter.xpath(ppath)dict[i] = s[0]print (s[0])print(dict)dictJson=dumps(dict,indent=4,ensure_ascii=False) print(dictJson) #寫出到j(luò)son文件 with open("result.txt", "w") as fp:fp.write(dictJson)

beautiful soup

import urllib from bs4 import BeautifulSoupwith urllib.request.urlopen("https://weixin.sogou.com/") as url:res = url.read() soup = BeautifulSoup(res,"html.parser") book_div = soup.find(attrs={"id":"topwords"}) i=0 for t in book_div.find_all('a'):i+=1;print(i, t.get('title')) #輸出json方法同上

lxml庫(kù)的安裝:https://blog.csdn.net/weixin_45203459/article/details/102577999

總結(jié)

以上是生活随笔為你收集整理的行业大数据爬虫练习的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。