Python无法用requests获取网页源码的解决方法

 更新时间:2022年07月08日 09:47:14   作者:henanlion  
爬虫获取信息,很多时候是需要从网页源码中获取链接信息的,下面这篇文章主要给大家介绍了关于Python无法用requests获取网页源码的解决方法,文中通过示例代码介绍的非常详细,需要的朋友可以参考下

最近在抓取http://skell.sketchengine.eu网页时,发现用requests无法获得网页的全部内容,所以我就用selenium先模拟浏览器打开网页,再获取网页的源代码,通过BeautifulSoup解析后拿到网页中的例句,为了能让循环持续进行,我们在循环体中加了refresh(),这样当浏览器得到新网址时通过刷新再更新网页内容,注意为了更好地获取网页内容,设定刷新后停留2秒,这样可以降低抓不到网页内容的机率。为了减少被封的可能,我们还加入了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')
 
# 创建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)
    # 刷新网页获取新数据
    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. 为了加快访问速度,我们设置不显示浏览器,通过chrome.options实现

2. 最近通过re正则表达式来清理格式。

3. 我们设置table[0:6]来获取前三个句子的内容,最后显示结果如下。

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. 

补充:经过代码的优化以后,例句的爬取更加快捷,代码如下:

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')
 
# 创建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")

总结

到此这篇关于Python无法用requests获取网页源码的文章就介绍到这了,更多相关requests获取网页源码内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python绘图实现台风路径可视化代码实例

    Python绘图实现台风路径可视化代码实例

    这篇文章主要介绍了Python绘图实现台风路径可视化代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-10-10
  • Python 利用邮件系统完成远程控制电脑的实现(关机、重启等)

    Python 利用邮件系统完成远程控制电脑的实现(关机、重启等)

    这篇文章主要介绍了Python 利用邮件系统完成远程控制电脑(关机、重启等),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • 浅谈Python numpy创建空数组的问题

    浅谈Python numpy创建空数组的问题

    今天遇到一个小小的问题,是关于numpy创建空数组,今天特地整理了这篇文章,文中作出了非常详细的介绍,对正在学习python的小伙伴们有很好的帮助,需要的朋友可以参考下
    2021-05-05
  • Python+Turtle动态绘制一棵树实例分享

    Python+Turtle动态绘制一棵树实例分享

    这篇文章主要介绍了Python+Turtle动态绘制一棵树实例分享,具有一定借鉴价值,需要的朋友可以参考下
    2018-01-01
  • Python中的Function定义方法

    Python中的Function定义方法

    Python中,函数是可被重用的程序段。对于函数的定义,可以使用def关键字。
    2009-09-09
  • Python图像运算之图像灰度非线性变换详解

    Python图像运算之图像灰度非线性变换详解

    这篇文章将详细讲解图像灰度非线性变换。图像灰度非线性变换主要包括对数变换、幂次变换、指数变换、分段函数变换,通过非线性关系对图像进行灰度处理,本文主要讲解三种常见类型的灰度非线性变换,感兴趣的可以了解一下
    2022-03-03
  • tensorflow实现图像的裁剪和填充方法

    tensorflow实现图像的裁剪和填充方法

    今天小编就为大家分享一篇tensorflow实现图像的裁剪和填充方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • Python定义二叉树及4种遍历方法实例详解

    Python定义二叉树及4种遍历方法实例详解

    这篇文章主要介绍了Python定义二叉树及4种遍历方法,结合实例形式较为详细的分析了二叉树的概念、原理,以及Python定义与遍历二叉树相关操作技巧,需要的朋友可以参考下
    2018-07-07
  • Python通过matplotlib绘制动画简单实例

    Python通过matplotlib绘制动画简单实例

    这篇文章主要介绍了Python通过matplotlib绘制动画简单实例,具有一定借鉴价值,需要的朋友可以参考下。
    2017-12-12
  • pycharm 使用心得(三)Hello world!

    pycharm 使用心得(三)Hello world!

    作为PyCharm编辑器的起步,我们理所当然的先写一个Hello word,并运行它。(此文献给对IDE不熟悉的初学者)
    2014-06-06

最新评论