Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Python crawl the joke collection

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Xiaobian to share with you Python how to crawl jokes, I believe most people do not know how, so share this article for everyone's reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

code

import requestfrom bs4 import BeautifulSoupheaders={ 'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3775.400 QQBrowser/10.6.4208.400'}#request header, crawler masquerade for i in range(0,100): url = 'http://xiaohua.zol.com.cn/detail15/{}.html'.format(i) #Crawler target website html = request.get(url, headers=headers) #Source code returned after request # html.encoding = 'utf-8' soup = BeautifulSoup(html.text, 'lxml')#parse source code if html.status_code==200: #Visit successfully title = soup.select(".article-title")[0].text.replace(' ', '') content = soup.select(".article-text")[0].text.replace(' ', '') with open('D:/xh.txt', 'a',encoding='utf-8') as f: #Save file in D:/xh.txt file f.write(title) f.write(content) f.write('\n\n') f.close() print(title, content) else: #Access failed Continue The above is "Python how to crawl jokes" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report