最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python無法用requests獲取網(wǎng)頁源碼的解決方法

 更新時間:2022年07月08日 09:47:14   作者:henanlion  
爬蟲獲取信息,很多時候是需要從網(wǎng)頁源碼中獲取鏈接信息的,下面這篇文章主要給大家介紹了關(guān)于Python無法用requests獲取網(wǎng)頁源碼的解決方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下

最近在抓取http://skell.sketchengine.eu網(wǎng)頁時,發(fā)現(xiàn)用requests無法獲得網(wǎng)頁的全部內(nèi)容,所以我就用selenium先模擬瀏覽器打開網(wǎng)頁,再獲取網(wǎng)頁的源代碼,通過BeautifulSoup解析后拿到網(wǎng)頁中的例句,為了能讓循環(huán)持續(xù)進(jìn)行,我們在循環(huán)體中加了refresh(),這樣當(dāng)瀏覽器得到新網(wǎng)址時通過刷新再更新網(wǎng)頁內(nèi)容,注意為了更好地獲取網(wǎng)頁內(nèi)容,設(shè)定刷新后停留2秒,這樣可以降低抓不到網(wǎng)頁內(nèi)容的機(jī)率。為了減少被封的可能,我們還加入了Chrome,請看以下代碼:

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實例 。
 
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)頁獲取新數(shù)據(jù)
    driver.refresh()
    time.sleep(2)
    # page_source——》獲得頁面源碼
    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. 為了加快訪問速度,我們設(shè)置不顯示瀏覽器,通過chrome.options實現(xiàn)

2. 最近通過re正則表達(dá)式來清理格式。

3. 我們設(shè)置table[0:6]來獲取前三個句子的內(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)過代碼的優(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實例 。
 
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——》頁面源碼
        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")

總結(jié)

到此這篇關(guān)于Python無法用requests獲取網(wǎng)頁源碼的文章就介紹到這了,更多相關(guān)requests獲取網(wǎng)頁源碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python繪圖實現(xiàn)臺風(fēng)路徑可視化代碼實例

    Python繪圖實現(xiàn)臺風(fēng)路徑可視化代碼實例

    這篇文章主要介紹了Python繪圖實現(xiàn)臺風(fēng)路徑可視化代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-10-10
  • Python 利用郵件系統(tǒng)完成遠(yuǎn)程控制電腦的實現(xiàn)(關(guān)機(jī)、重啟等)

    Python 利用郵件系統(tǒng)完成遠(yuǎn)程控制電腦的實現(xiàn)(關(guān)機(jī)、重啟等)

    這篇文章主要介紹了Python 利用郵件系統(tǒng)完成遠(yuǎn)程控制電腦(關(guān)機(jī)、重啟等),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • 淺談Python numpy創(chuàng)建空數(shù)組的問題

    淺談Python numpy創(chuàng)建空數(shù)組的問題

    今天遇到一個小小的問題,是關(guān)于numpy創(chuàng)建空數(shù)組,今天特地整理了這篇文章,文中作出了非常詳細(xì)的介紹,對正在學(xué)習(xí)python的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-05-05
  • Python+Turtle動態(tài)繪制一棵樹實例分享

    Python+Turtle動態(tài)繪制一棵樹實例分享

    這篇文章主要介紹了Python+Turtle動態(tài)繪制一棵樹實例分享,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • Python中的Function定義方法

    Python中的Function定義方法

    Python中,函數(shù)是可被重用的程序段。對于函數(shù)的定義,可以使用def關(guān)鍵字。
    2009-09-09
  • Python圖像運算之圖像灰度非線性變換詳解

    Python圖像運算之圖像灰度非線性變換詳解

    這篇文章將詳細(xì)講解圖像灰度非線性變換。圖像灰度非線性變換主要包括對數(shù)變換、冪次變換、指數(shù)變換、分段函數(shù)變換,通過非線性關(guān)系對圖像進(jìn)行灰度處理,本文主要講解三種常見類型的灰度非線性變換,感興趣的可以了解一下
    2022-03-03
  • tensorflow實現(xiàn)圖像的裁剪和填充方法

    tensorflow實現(xiàn)圖像的裁剪和填充方法

    今天小編就為大家分享一篇tensorflow實現(xiàn)圖像的裁剪和填充方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Python定義二叉樹及4種遍歷方法實例詳解

    Python定義二叉樹及4種遍歷方法實例詳解

    這篇文章主要介紹了Python定義二叉樹及4種遍歷方法,結(jié)合實例形式較為詳細(xì)的分析了二叉樹的概念、原理,以及Python定義與遍歷二叉樹相關(guān)操作技巧,需要的朋友可以參考下
    2018-07-07
  • Python通過matplotlib繪制動畫簡單實例

    Python通過matplotlib繪制動畫簡單實例

    這篇文章主要介紹了Python通過matplotlib繪制動畫簡單實例,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • pycharm 使用心得(三)Hello world!

    pycharm 使用心得(三)Hello world!

    作為PyCharm編輯器的起步,我們理所當(dāng)然的先寫一個Hello word,并運行它。(此文獻(xiàn)給對IDE不熟悉的初學(xué)者)
    2014-06-06

最新評論

荔浦县| 延长县| 革吉县| 朝阳区| 仙居县| 无极县| 上犹县| 兴国县| 河间市| 常宁市| 泰宁县| 华池县| 和林格尔县| 岳池县| 根河市| 金平| 鹤峰县| 工布江达县| 阿拉善右旗| 葫芦岛市| 星子县| 克山县| 潜江市| 大足县| 临沧市| 堆龙德庆县| 江川县| 福安市| 宁波市| 吉木乃县| 新河县| 囊谦县| 舟山市| 资阳市| 翁牛特旗| 石阡县| 密山市| 西林县| 颍上县| 涿鹿县| 安福县|