Python下 selenium + GraphQuery 採集小例

現在反爬措施日新月異,爬蟲技術也道高一尺魔高一丈,經歷了IP封禁、js防爬等防禦手段,總結了一套還算是不錯的採集組合

GraphQuery: https://github.com/storyicon/graphquery

國內能查到的資料貌似不多,但是功能還是很強的,用一種類似於接口請求的方式去獲取所需的格式化數據。

selenium:

控制 chorme ,操作瀏覽器的一個工具;

基本原理是:

selenium獲取到數據,帶着想要的數據接口,直接轉給 GraphQuery ,獲取到想要的數據。

貼個代碼,寫的比較簡單,就是個臨時的小工具:

import requests
import json
from selenium import webdriver
# 先把所有的內容鏈接搞回來
# url = "http://yjj.sh.gov.cn/XingZhengChuFa/xxgk2.aspx?pu=&qymc=&slrqstart=&slrqend=&pageindex=1&pagesize=100"

driver_path = r"G:\chromedriver_win32\chromedriver.exe"
opt = webdriver.ChromeOptions()

opt.add_argument('--headless')
opt.add_argument('--disable-gpu')

driver = webdriver.Chrome(executable_path=driver_path, options=opt)



res = []
# 傳入鏈接
def GraphQuery(document, expr):
    response = requests.post("http://127.0.0.1:8559", data={
        "document": document,
        "expression": expr,
    })
    return response.text

def go(url):
    driver.get(url)
    content = driver.page_source
    conseq = GraphQuery(content, r"""
                    {
                        url `css("table a")` [u `attr("href")`]
                    }
                """)
    count = json.loads(conseq)
    #print(count)
    # 把內容鏈接循環遍歷打開
    for i in count['data']['url']:
        u = 'http://yjj.sh.gov.cn/XingZhengChuFa/'+i
        # 檢查頁面內是否存在 色拉 沙拉 相關數據
        i = requests.get(u)
        content = i.content.decode("UTF-8")
        keys = ["沙拉", "色拉"]
        for key in keys:
            if key in content:
                # 存在則記錄 鏈接
                res.append(u)
                f1 = open('test.txt', 'a+')
                f1.write(u+'\n')

                f1.close()


i = 100
while i < 620:
    url = "http://yjj.sh.gov.cn/XingZhengChuFa/xxgk2.aspx?pu=&qymc=&slrqstart=&slrqend=&pageindex="+i.__str__()+"&pagesize=50"
    try:
        i += 1
        print(i)
        go(url)
        print(res)
    except:
        continue

print(res)

 

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