日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

【Python爬虫】写个爬虫爬取自己的博客,可以刷访问量

發布時間:2024/2/28 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Python爬虫】写个爬虫爬取自己的博客,可以刷访问量 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

預備工作

添加外部包:

pip install bs4 pip install requests pip install virtualenv(這個好像沒有必要) pip install lxml

第一步:爬取自己首頁的博客鏈接


代碼

# coding: utf-8 import re import requests from bs4 import BeautifulSoupdef get_blog_info():headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) ''AppleWebKit/537.36 (KHTML, like Gecko) ''Ubuntu Chromium/44.0.2403.89 ''Chrome/44.0.2403.89 ''Safari/537.36'}html = get_page(blog_url)soup = BeautifulSoup(html, 'lxml')article_list = soup.find('main')article_item = article_list.find_all('p', attrs={'class': 'content'})for ai in article_item:title = ai.a.textlink = ai.a['href']#print(title)print(link)#write_to_file(title+'\t')#write_to_file(link+'\n')def get_page(url):try:headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) ''AppleWebKit/537.36 (KHTML, like Gecko) ''Ubuntu Chromium/44.0.2403.89 ''Chrome/44.0.2403.89 ''Safari/537.36'}response = requests.get(blog_url, headers=headers, timeout=10)return response.textexcept:return ""def write_to_file(content):with open('article.txt', 'a', encoding='utf-8') as f:f.write(content)if __name__ == '__main__':blog_url = "https://blog.csdn.net/sinat_42483341?t=1"get_blog_info()

第二步:通過request訪問這些鏈接

只是request請求,不進行任何操作,相當于訪問自己的每一篇博客

代碼

# coding: utf-8 import re import requests from bs4 import BeautifulSoupdef get_page():try:headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) ''AppleWebKit/537.36 (KHTML, like Gecko) ''Ubuntu Chromium/44.0.2403.89 ''Chrome/44.0.2403.89 ''Safari/537.36'}for blog_url in blog_urls:response = requests.get(blog_url, headers=headers, timeout=10)print("url="+blog_url)#return response.textexcept:return ""def write_to_file(content):with open('article.txt', 'a', encoding='utf-8') as f:f.write(content)if __name__ == '__main__':blog_urls = ["https://blog.csdn.net/sinat_42483341/article/details/91826523","https://blog.csdn.net/sinat_42483341/article/details/89931215",'https://blog.csdn.net/sinat_42483341/article/details/89034286', 'https://blog.csdn.net/sinat_42483341/article/details/88849892', 'https://blog.csdn.net/sinat_42483341/article/details/95871910', 'https://blog.csdn.net/sinat_42483341/article/details/95768679', 'https://blog.csdn.net/sinat_42483341/article/details/95495296', 'https://blog.csdn.net/sinat_42483341/article/details/95043847', 'https://blog.csdn.net/sinat_42483341/article/details/95014941', 'https://blog.csdn.net/sinat_42483341/article/details/94969983', 'https://blog.csdn.net/sinat_42483341/article/details/94492282', 'https://blog.csdn.net/sinat_42483341/article/details/94443619', 'https://blog.csdn.net/sinat_42483341/article/details/94388710', 'https://blog.csdn.net/sinat_42483341/article/details/94296696', 'https://blog.csdn.net/sinat_42483341/article/details/94133323', 'https://blog.csdn.net/sinat_42483341/article/details/94053208', 'https://blog.csdn.net/sinat_42483341/article/details/94050774', 'https://blog.csdn.net/sinat_42483341/article/details/93769801', 'https://blog.csdn.net/sinat_42483341/article/details/93746360', 'https://blog.csdn.net/sinat_42483341/article/details/93739451']get_page()

第三步:(待寫)

想要達到自動刷訪問量的效果,當然不可能每次都自己手動把字符串存到數組中。應該把所有鏈接自動存進數組里,逐個訪問即可,懶得寫了,不過已經寫好了Java版,可以參考:

【Java爬蟲】自己寫爬蟲練手,刷CSDN訪問量

總結

以上是生活随笔為你收集整理的【Python爬虫】写个爬虫爬取自己的博客,可以刷访问量的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。