爬取網址:www.51job.com
1.首先我們來分析需要爬取網站的情況
在51job中輸入:數據分析師
具體過程詳解,請移步:https://blog.csdn.net/qq_31848185/article/details/88967352
貼上我的過程,兩種方式兩種思路,沒有好壞之分,共同學習,在此再一次感謝,前一位博主!
from requests_html import HTMLSession
import time
session = HTMLSession()
#定義一個url函數,自主輸入要爬取的頁數
def my_url(m):
while m>0:
p=m-(m-1)
url = 'https://search.51job.com/list/000000,000000,0000,00,9,99,%25E6%2595%25B0%25E6%258D%25AE%25E5%2588%2586%25E6%259E%2590%25E5%25B8%2588,2,{}.html'.format(p)
r = session.get(url)
for n in range(4,54):
workname = r.html.xpath(('//*[@id="resultList"]/div[{}]/p/span/a/@title').format(n))##職位
company=r.html.xpath(('//*[@id="resultList"]/div[{}]/span[1]/a/@title').format(n))#公司名
address=r.html.xpath(('//*[@id="resultList"]/div[{}]/span[2]/text()').format(n))#工作地點
money=r.html.xpath(('//*[@id="resultList"]/div[{}]/span[3]/text()').format(n))#薪資
time1 = r.html.xpath(('//*[@id="resultList"]/div[{}]/span[4]/text()').format(n)) # 薪資
print("第(",(n-3),")條:",workname,company,address,money)
time.sleep(1) # 時間間隔。防止被封IP
p=p+1
m=m-1
print(m)
print(p)
else:
return
if __name__=='__main__':
num=int(input('請輸入要爬取的頁數:'))#輸入要爬取的頁數
my_url(num)