您现在的位置是:首页 > 编程语言学习 > 前端编程语言 > 文章正文 前端编程语言

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

2022-07-09 10:01:08 前端编程语言

简介最近在抓取http://skell.sketchengine.eu网页时,发现用requests无法获得网页的全部内容,所以我就用selenium先模拟浏览器打开网页,再获取...

最近在抓取http://skell.sketchengine.eu网页时,发现用requests无法获得网页的全部内容,所以我就用selenium先模拟浏览器打开网页,再获取网页的源代码,通过BeautifulSoup解析后拿到网页中的例句。

为了能让循环持续进行,我们在循环体中加了refresh(),这样当浏览器得到新网址时通过刷新再更新网页内容,注意为了更好地获取网页内容,设定刷新后停留2秒,这样可以降低抓不到网页内容的机率。为了减少被封的可能,我们还加入了Chrome,请看以下代码:

  1. from selenium import webdriver 
  2. from selenium.webdriver.chrome.options import Options 
  3. from selenium.webdriver.chrome.service import Service 
  4. from bs4 import BeautifulSoup 
  5. import time,re 
  6.   
  7. path = Service("D:\\MyDrivers\\chromedriver.exe")# 
  8. # 配置不显示浏览器 
  9. chrome_options = Options() 
  10. chrome_options.add_argument('--headless'
  11. chrome_options.add_argument('--disable-gpu'
  12. 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'
  13.   
  14. # 创建Chrome实例 。 
  15.   
  16. driver = webdriver.Chrome(service=path,options=chrome_options) 
  17. lst=["happy","help","evening","great","think","adapt"
  18.   
  19. for word in lst: 
  20.     url="https://skell.sketchengine.eu/#result?lang=en&query="+word+"&f=concordance" 
  21.     driver.get(url) 
  22.     # 刷新网页获取新数据 
  23.     driver.refresh() 
  24.     time.sleep(2) 
  25.     # page_source——》获得页面源码 
  26.     resp=driver.page_source 
  27.     # 解析源码 
  28.     soup=BeautifulSoup(resp,"html.parser"
  29.     table = soup.find_all("td"
  30.     with open("eps.txt",'a+',encoding='utf-8') as f: 
  31.         f.write(f"\n{word}的例子\n"
  32.     for i in table[0:6]: 
  33.         text=i.text 
  34.         #替换多余的空格 
  35.         new=re.sub("\s+"," ",text) 
  36.         #写入txt文本 
  37.         with open("eps.txt",'a+',encoding='utf-8') as f: 
  38.             f.write(re.sub(r"^(\d+\.)",r"\n\1",new)) 
  39. 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.

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

  1. from selenium import webdriver 
  2. from selenium.webdriver.chrome.options import Options 
  3. from selenium.webdriver.chrome.service import Service 
  4. from bs4 import BeautifulSoup 
  5. import time,re 
  6. import os 
  7.   
  8. # 配置模拟浏览器的位置 
  9. path = Service("D:\\MyDrivers\\chromedriver.exe")# 
  10. # 配置不显示浏览器 
  11. chrome_options = Options() 
  12. chrome_options.add_argument('--headless'
  13. chrome_options.add_argument('--disable-gpu'
  14. 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'
  15.   
  16. # 创建Chrome实例 。 
  17.   
  18. def get_wordlist(): 
  19. wordlist=[] 
  20. with open("wordlist.txt",'r',encoding='utf-8') as f: 
  21. lines=f.readlines() 
  22. for line in lines: 
  23. word=line.strip() 
  24. wordlist.append(word) 
  25. return wordlist 
  26.   
  27. def main(lst): 
  28. driver = webdriver.Chrome(service=path,options=chrome_options) 
  29. for word in lst: 
  30. url="https://skell.sketchengine.eu/#result?lang=en&query="+word+"&f=concordance" 
  31. driver.get(url)  
  32. driver.refresh() 
  33. time.sleep(2) 
  34. # page_source——》页面源码 
  35. resp=driver.page_source 
  36. # 解析源码 
  37. soup=BeautifulSoup(resp,"html.parser"
  38. table = soup.find_all("td"
  39. with open("examples.txt",'a+',encoding='utf-8') as f: 
  40. f.writelines(f"\n{word}的例子\n"
  41. for i in table[0:6]: 
  42. text=i.text 
  43. new=re.sub("\s+"," ",text) 
  44. with open("eps.txt",'a+',encoding='utf-8') as f: 
  45. f.write(new
  46. # f.writelines(re.sub("(\.\s)(\d+\.)","\1\n\2",new)) 
  47.   
  48. if __name__=="__main__"
  49. lst=get_wordlist() 
  50. main(lst) 
  51. os.startfile("examples.txt"

 

站点信息