鍍金池/ 問(wèn)答/數(shù)據(jù)分析&挖掘  Python/ 請(qǐng)教一個(gè)爬蟲(chóng)相關(guān)的問(wèn)題

請(qǐng)教一個(gè)爬蟲(chóng)相關(guān)的問(wèn)題

藥監(jiān)局網(wǎng)站

http://app2.sfda.gov.cn/datas...

有需求需要從藥監(jiān)局網(wǎng)站抓取一點(diǎn)數(shù)據(jù),發(fā)現(xiàn)藥監(jiān)局網(wǎng)站反爬蟲(chóng)無(wú)法破解(狀態(tài)碼返回 202,無(wú)法抓到正確的 html),想求教一下各位大佬有沒(méi)有解決辦法?

已經(jīng)嘗試過(guò)的方法:
1.requests + fake_useragent
2.PhantomJS + selenium
3.requests_html 的 render() (pyppeteer+Chromium)

回答
編輯回答
赱丅呿

python 3.5環(huán)境 把chromedriver的路徑改成自己的就可以運(yùn)行了

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait

chrome_opt = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images":2}
chrome_opt.add_experimental_option("prefs", prefs)
browser = webdriver.Chrome(executable_path="E:\selenium\chromedriver.exe", chrome_options=chrome_opt)
browser.set_window_position(-10, 0)
pageNum = 1
countNum = 1
while True:

print("\n當(dāng)前頁(yè)數(shù):%d"%pageNum)
browser.get("http://app2.sfda.gov.cn/datasearchp/gzcxSearch.do?page=%d&searchcx=&optionType=V1&paramter0=null&paramter1=null&paramter2=null&formRender=cx"%pageNum)
browser.execute_script("var q=document.documentElement.scrollTop=270")
allProduct = WebDriverWait(browser, 10).until(lambda x: x.find_elements_by_xpath('/html/body/center/table[4]/tbody/tr[2]/td/center/table/tbody/tr[3]/td/table[1]/tbody/tr'))
for product in allProduct:
    if (product.get_attribute("name") != None):
        cpmc = WebDriverWait(product, 10).until(lambda x: x.find_element_by_xpath('./td[1]')).text
        gcyp = WebDriverWait(product, 10).until(lambda x: x.find_element_by_xpath('./td[2]/table/tbody/tr/td[2]/table/tbody/tr/td[2]/font')).text
        jkyp = WebDriverWait(product, 10).until(lambda x: x.find_element_by_xpath('./td[2]/table/tbody/tr/td[4]/table/tbody/tr/td[2]/font')).text
        ypgg = WebDriverWait(product, 10).until(lambda x: x.find_element_by_xpath('./td[2]/table/tbody/tr/td[6]/table/tbody/tr/td[2]/font')).text
        print("編號(hào):%s, 產(chǎn)品名稱(chēng):%s, 國(guó)產(chǎn)藥品:%s, 進(jìn)口藥品:%s, 藥品廣告:%s"%(countNum, cpmc, gcyp, jkyp, ypgg))
        countNum += 1
if (len(allProduct) != 30) : break
pageNum += 1
2017年10月31日 16:04
編輯回答
冷眸

用requests帶cookie去訪問(wèn)是可以得到200的,看了一下cookie獲取過(guò)程比較復(fù)雜,簡(jiǎn)單快速的直接用selenium就可以了,樓上代碼親測(cè)可行

2017年8月12日 09:01