【python小項目】抓取編程網收費vip文章的非vip用戶觀看界面的url


#!/usr/bin/python3
#coding=UTF-8
import requests
from bs4 import BeautifulSoup

'''
需求:【python小項目】抓取編程網收費vip文章的非vip用戶觀看界面的url! 例如收費文章http://c.biancheng.net/view/vip_6005.html對應非收費地址是http://c.biancheng.net/view/5315.html這個網站總是有一些vip文章  但是vip文章通過百度標題是可以搜索到的,我想爬取所有這樣的文章標題和網頁的地址!後期看到一個vip文章,你可以通過檢索標題得到非vip的觀看鏈接地址
編寫日期:2019-10-18
作者:xiaoxiaohui
說明:python3程序 而且最好在linux運行 windows下有gbk那個編碼問題
'''

def get_biaoti(url):
	response = requests.get(url)
	response.encoding='utf-8' #如果不設置成utf8則中文亂碼或者報錯 參考https://www.cnblogs.com/supery007/p/8303472.html
	soup = BeautifulSoup(response.text,'html.parser')
	links_div = soup.find_all('h1')
	return links_div[0].text

f = open("a1.txt", 'a')
for yema in range(1,500):
	url = 'http://c.biancheng.net/view/'+str(yema)+'.html'
	biaoti = get_biaoti(url)
	print(url,biaoti) 
	f.write(url+'\t'+biaoti+'\n')
f.close()


運行結果、收集到的文章和url對應關係截圖:

運行結果圖.png

自己學到的:

  1. 這次學到的  原來我的爬蟲都是爬某個div的,都是links_div = soup.find_all('div',class_="listpic"), 原來也可以直接links_div = soup.find_all('h1'),也就是帶一個參數這樣的




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章