2020年2月17日
、最近のコンピュータが壊れている、私はかわいそうな新しいpro13ああの3ヶ月、質問のうち、ディスプレイを買いました。私は(角度は、他のは、実際に最も暗い明るさで、上に横たわっていることは漠然と、蜂蜜の問題の概要を見ることができます)ディスプレイは、特定の角度で光のみ表示されます、黒い画面が開かれ、どこが壊れているかわかりません、私はコンピュータの左下隅が私の左手の位置が、それは比較的正常に表示され重く抑制を使用しているほとんどのものを示して???本当に私は問題が何であるかを理解していないが、今の腕の長さで、Lenovoコンピューターの未来。最近流行ための、Lenovoのアフターマーケットでは、ドアを開くことはないキノコです。
まあ、学習プロセスの多くは、純粋に不満を発散することで、コンピュータがハード私を作ると述べました。
ただクロール爬虫類は、今日の見出しのコード補完の本当の絵を開発していた、我々は、コードの本をあるいくつかのサイトの変更は、クロールされないことがわかりました。二つの主要な変更、クロール写真はクッキーを必要とする模倣する1)AJAXリクエストがありますが、2)JSONコンテンツばらつくが返され、そして何image_detailフィールドがありません。以下は、特定のコンテンツです
1)クッキーの追加
最初のページ要求XHRクッキーがで取得する通りに撮影することができますを
して、最初の要求のXHRがクッキーを設定するための応答フィールドのimg要求がある見ることができる前に、と長い時間を満了し、有効期限が切れそれは、再びそれにクッキーを訪れコピーします。
だから、次のようにクッキーを設定した後、ヘッダを持ちます。
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0",
"Referer":"https://www.toutiao.com/search/?keyword=%E8%A1%97%E6%8B%8D",
"X-Requested-With":"XMLHttpRequest",
"Cookie":"__tasessionId=da6dc6d4q1581851848391; s_v_web_id=k6oxqyas_VGn20UCx_WXjQ_40eQ_9nhD_h0a5HUmjAsyD; csrftoken=cdcf90d6d3d490ab1326e261b2eff18a; tt_webid=6794001919121065486"
}
2)フォーマットは、JSONコンテンツの変更が返さ
データフィールド、フィールドIMAGE_LISTにimage_detailないが、データフィールドにそれぞれがIMAGE_LISTを持っていません
特定コード:
コードセクションは、3つの部分、主要な機能に分割されている; get_one_page機能:取得JSONオフセット範囲内のデータ、および戻り、発電機であり、主に通過できる。save_img機能:IMGビルダーから得られました情報とURLを保存
import requests, json, time,os
from lxml import etree
from requests import RequestException
from hashlib import md5
base_url1 = "https://www.toutiao.com/api/search/content/?aid=24&app_name=web_search&offset="
base_url2 = "&format=json&keyword=%E8%A1%97%E6%8B%8D&autoload=true&count=20&en_qc=1&cur_tab=1&from=search_tab&pd=synthesis×tamp="
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0",
"Referer":"https://www.toutiao.com/search/?keyword=%E8%A1%97%E6%8B%8D",
"X-Requested-With":"XMLHttpRequest",
"Cookie":"__tasessionId=da6dc6d4q1581851848391; s_v_web_id=k6oxqyas_VGn20UCx_WXjQ_40eQ_9nhD_h0a5HUmjAsyD; csrftoken=cdcf90d6d3d490ab1326e261b2eff18a; tt_webid=6794001919121065486"
}
base_save_path = "D:/toutiao/"
def get_one_page(base_url:str)->dict:
'''
@base_url: request url
@return: nop
'''
try:
resp = requests.get(base_url, headers = headers)
except RequestException:
return None
json_content = resp.json()
print(base_url, '\n')
# print(json.dumps(json_content, indent=2, ensure_ascii=False))
data = json_content.get("data")
for item in data:
title = item.get("title")
imgs = item.get("image_list")
if (imgs):
title_img = {}
title_img["title"] = title
title_img["image_list"] = imgs
yield title_img
else:
continue
def save_img(title_img: dict):
title = title_img.get("title")
title_path = base_save_path +title
if not os.path.exists(title_path):
os.mkdir(title_path)
for img in title_img.get("image_list"):
img_url = img.get("url")
img_resp = requests.get(img_url)
if img_resp.status_code == 200:
file_path = '{0}{1}.{2}'.format(title_path + '/', md5(img_resp.content).hexdigest(), 'jpg')
if not os.path.exists(file_path):
with open(file_path,'wb') as img_f:
img_f.write(img_resp.content)
else:
print("already downloaded :", file_path)
def main():
for i in range(0, 120, 20):
base_url = base_url1 + str(i) + base_url2 + str(time.time()).replace('.','')[:-4]
title_imgs = get_one_page(base_url)
for title_img in title_imgs:
save_img(title_img)
time.sleep(1)
if __name__ == "__main__":
main()