其實(shí),我們?cè)诠ぷ魃町?dāng)中,總是會(huì)遇到各種各樣的困惑的,甚至遇到各種麻煩,一旦遇到這么麻煩問(wèn)題的話,就會(huì)嚴(yán)重的影響到我們的工作效率,比如遇到Python無(wú)法用requests獲取網(wǎng)頁(yè)源碼的問(wèn)題,那么,我們?cè)撛趺慈ミM(jìn)行解決呢?下面就給大家詳細(xì)解答下。
最近在抓取http://skell.sketchengine.eu網(wǎng)頁(yè)時(shí),發(fā)現(xiàn)用requests無(wú)法獲得網(wǎng)頁(yè)的全部?jī)?nèi)容,所以我就用selenium先模擬瀏覽器打開網(wǎng)頁(yè),再獲取網(wǎng)頁(yè)的源代碼,通過(guò)BeautifulSoup解析后拿到網(wǎng)頁(yè)中的例句,為了能讓循環(huán)持續(xù)進(jìn)行,我們?cè)谘h(huán)體中加了refresh(),這樣當(dāng)瀏覽器得到新網(wǎng)址時(shí)通過(guò)刷新再更新網(wǎng)頁(yè)內(nèi)容,注意為了更好地獲取網(wǎng)頁(yè)內(nèi)容,設(shè)定刷新后停留2秒,這樣可以降低抓不到網(wǎng)頁(yè)內(nèi)容的機(jī)率。為了減少被封的可能,我們還加入了Chrome,請(qǐng)看以下代碼:
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from bs4 import BeautifulSoup import time,re path=Service("D:\\MyDrivers\\chromedriver.exe")# #配置不顯示瀏覽器 chrome_options=Options() chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-gpu') chrome_options.add_argument('User-Agent="Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,like Gecko)Chrome/97.0.4692.99 Safari/537.36') #創(chuàng)建Chrome實(shí)例。 driver=webdriver.Chrome(service=path,options=chrome_options) lst=["happy","help","evening","great","think","adapt"] for word in lst: url="https://skell.sketchengine.eu/#result?lang=en&query="+word+"&f=concordance" driver.get(url) #刷新網(wǎng)頁(yè)獲取新數(shù)據(jù) driver.refresh() time.sleep(2) #page_source——》獲得頁(yè)面源碼 resp=driver.page_source #解析源碼 soup=BeautifulSoup(resp,"html.parser") table=soup.find_all("td") with open("eps.txt",'a+',encoding='utf-8')as f: f.write(f"\n{word}的例子\n") for i in table[0:6]: text=i.text #替換多余的空格 new=re.sub("\s+","",text) #寫入txt文本 with open("eps.txt",'a+',encoding='utf-8')as f: f.write(re.sub(r"^(\d+\.)",r"\n\1",new)) driver.close()
1.為了加快訪問(wèn)速度,我們?cè)O(shè)置不顯示瀏覽器,通過(guò)chrome.options實(shí)現(xiàn)
2.最近通過(guò)re正則表達(dá)式來(lái)清理格式。
3.我們?cè)O(shè)置table[0:6]來(lái)獲取前三個(gè)句子的內(nèi)容,最后顯示結(jié)果如下。
happy的例子
1.This happy mood lasted roughly until last autumn.
2.The lodging was neither convenient nor happy.
3.One big happy family"fighting communism".
help的例子
1.Applying hot moist towels may help relieve discomfort.
2.The intense light helps reproduce colors more effectively.
3.My survival route are self help books.
evening的例子
1.The evening feast costs another$10.
2.My evening hunt was pretty flat overall.
3.The area nightclubs were active during evenings.
great的例子
1.The three countries represented here are three great democracies.
2.Our three different tour guides were great.
3.Your receptionist"crew"is great!
think的例子
1.I said yes immediately without thinking everything through.
2.This book was shocking yet thought provoking.
3.He thought"disgusting"was more appropriate.
adapt的例子
1.The novel has been adapted several times.
2.There are many ways plants can adapt.
3.They must adapt quickly to changing deadlines.
補(bǔ)充:經(jīng)過(guò)代碼的優(yōu)化以后,例句的爬取更加快捷,代碼如下:
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from bs4 import BeautifulSoup import time,re import os #配置模擬瀏覽器的位置 path=Service("D:\\MyDrivers\\chromedriver.exe")# #配置不顯示瀏覽器 chrome_options=Options() chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-gpu') chrome_options.add_argument('User-Agent="Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,like Gecko)Chrome/97.0.4692.99 Safari/537.36') #創(chuàng)建Chrome實(shí)例。 def get_wordlist(): wordlist=[] with open("wordlist.txt",'r',encoding='utf-8')as f: lines=f.readlines() for line in lines: word=line.strip() wordlist.append(word) return wordlist def main(lst): driver=webdriver.Chrome(service=path,options=chrome_options) for word in lst: url="https://skell.sketchengine.eu/#result?lang=en&query="+word+"&f=concordance" driver.get(url) driver.refresh() time.sleep(2) #page_source——》頁(yè)面源碼 resp=driver.page_source #解析源碼 soup=BeautifulSoup(resp,"html.parser") table=soup.find_all("td") with open("examples.txt",'a+',encoding='utf-8')as f: f.writelines(f"\n{word}的例子\n") for i in table[0:6]: text=i.text new=re.sub("\s+","",text) with open("eps.txt",'a+',encoding='utf-8')as f: f.write(new) #f.writelines(re.sub("(\.\s)(\d+\.)","\1\n\2",new)) if __name__=="__main__": lst=get_wordlist() main(lst) os.startfile("examples.txt")
到此為止,關(guān)于Python無(wú)法用requests獲取網(wǎng)頁(yè)源碼的問(wèn)題,就為大家介紹到這里了,希望可以為大家?guī)?lái)更多的幫助。
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/128059.html
摘要:關(guān)于本教程有任何建議或者疑問(wèn),都?xì)g迎郵件與我聯(lián)系,或者在上提出教程流程簡(jiǎn)介教程將會(huì)從如何分析微信協(xié)議開始,第一部分將教你如何從零開始獲取并模擬擴(kuò)展個(gè)人微信號(hào)所需要的協(xié)議。 現(xiàn)在的日常生活已經(jīng)離不開微信,難免會(huì)生出微信有沒有什么API可以使用的想法。 那樣就可以拿自己微信做個(gè)消息聚合、開個(gè)投票什么的,可以顯然沒有這種東西。 不過(guò)還好,有網(wǎng)頁(yè)版微信不就等于有了API么,這個(gè)項(xiàng)目就是出于這個(gè)...
摘要:編碼我們發(fā)現(xiàn),中有時(shí)候存在中文,這是就需要對(duì)進(jìn)行編碼??梢韵葘⒅形霓D(zhuǎn)換成編碼,然后使用方法對(duì)參數(shù)進(jìn)行編碼后傳遞。 本文檔對(duì)日常學(xué)習(xí)中用 python 做數(shù)據(jù)爬取時(shí)所遇到的一些問(wèn)題做簡(jiǎn)要記錄,以便日后查閱,部分問(wèn)題可能因?yàn)檎J(rèn)識(shí)不到位會(huì)存在一些誤解,敬請(qǐng)告知,萬(wàn)分感謝,共同進(jìn)步。 估算網(wǎng)站規(guī)模 該小節(jié)主要針對(duì)于整站爬取的情況。爬取整站之前,肯定是要先對(duì)一個(gè)網(wǎng)站的規(guī)模進(jìn)行估計(jì)。這是可以使用g...
摘要:總的來(lái)說(shuō)有兩種反爬策略,要么驗(yàn)證身份,把蟲子踩死在門口要么在網(wǎng)站植入各種反爬機(jī)制,讓爬蟲知難而退。本節(jié)內(nèi)容就著這兩種反爬策略提出一些對(duì)策。內(nèi)嵌反爬很靈活,沒有什么固定的代碼格式,要花時(shí)間去分析出來(lái)。 ??之前提到過(guò),有些網(wǎng)站是防爬蟲的。其實(shí)事實(shí)是,凡是有一定規(guī)模的網(wǎng)站,大公司的網(wǎng)站,或是盈利性質(zhì)比較強(qiáng)的網(wǎng)站,都是有高級(jí)的防爬措施的??偟膩?lái)說(shuō)有兩種反爬策略,要么驗(yàn)證身份,把蟲子踩死在門口...
摘要:項(xiàng)目作用訪問(wèn)項(xiàng)目的網(wǎng)頁(yè),掃一掃網(wǎng)頁(yè)上的二維碼,就會(huì)顯示你的微信好友中將你刪除的人的列表。顯示參考文檔該功能的實(shí)現(xiàn)網(wǎng)頁(yè)微信登錄原理項(xiàng)目源碼項(xiàng)目源碼 項(xiàng)目作用 訪問(wèn)項(xiàng)目的網(wǎng)頁(yè),掃一掃網(wǎng)頁(yè)上的二維碼,就會(huì)顯示你的微信好友中將你刪除的人的列表。 在線網(wǎng)址: 訪問(wèn)115.29.55.54:8080/WXApi就可以使用該項(xiàng)目所說(shuō)的網(wǎng)頁(yè) 項(xiàng)目原理 在微信中,將你刪掉的好友是無(wú)法加入你創(chuàng)建的群...
摘要:項(xiàng)目作用訪問(wèn)項(xiàng)目的網(wǎng)頁(yè),掃一掃網(wǎng)頁(yè)上的二維碼,就會(huì)顯示你的微信好友中將你刪除的人的列表。顯示參考文檔該功能的實(shí)現(xiàn)網(wǎng)頁(yè)微信登錄原理項(xiàng)目源碼項(xiàng)目源碼 項(xiàng)目作用 訪問(wèn)項(xiàng)目的網(wǎng)頁(yè),掃一掃網(wǎng)頁(yè)上的二維碼,就會(huì)顯示你的微信好友中將你刪除的人的列表。 在線網(wǎng)址: 訪問(wèn)115.29.55.54:8080/WXApi就可以使用該項(xiàng)目所說(shuō)的網(wǎng)頁(yè) 項(xiàng)目原理 在微信中,將你刪掉的好友是無(wú)法加入你創(chuàng)建的群...
閱讀 1066·2023-01-14 11:38
閱讀 1062·2023-01-14 11:04
閱讀 901·2023-01-14 10:48
閱讀 2377·2023-01-14 10:34
閱讀 1147·2023-01-14 10:24
閱讀 1026·2023-01-14 10:18
閱讀 654·2023-01-14 10:09
閱讀 733·2023-01-14 10:02