今日头条python_今日头条.py · 李强/Python-Crawler - Gitee.com

#coding=utf-8

import requests

from urllib.parse import urlencode

import os

from hashlib import md5

def get_page(offset,keyword):

params={

'offset':offset,

'format':'json',

'keyword':keyword,

'autoload':'true',

'count':'20',

'cur_tab':'1',

'from':'search_tab'

}

#https://www.toutiao.com/search_content/?offset=60&format=json&keyword=%E8%BD%A6%E6%A8%A1&autoload=true&count=20&cur_tab=1&from=search_tab

url='https://www.toutiao.com/search_content/?'+urlencode(params)

response=requests.get(url)

#500服务器内部错误,400错误请求(服务器找不到请求的语法) 404未找到

if response.status_code==200:

return response.json()

def get_images(json):

data=json.get('data')

if data:

for item in data:

image_list=item.get('image_list')

title=item.get('title')

if image_list:

for image in image_list:

#构造一个生成器,将图片和标题一起返回

yield {

'image':image.get('url'),

'title':title

}

#item就是get_image()返回的一个字典

#item里面的title创建一个文件夹

def save_image(item):

if not os.path.exists(item.get('title')):

os.mkdir(item.get('title'))

local_image_url=item.get('image')

response=requests.get("http:"+local_image_url)

if response.status_code==200:

file_path='{0}/{1}.{2}'.format(item.get('title'),md5(response.content).hexdigest(),'jpg')

#判断路径是否存在,如果不存在,写入

if not os.path.exists(file_path):

with open(file_path,'wb')as f:

f.write(response.content)

#定义一个offset数组,遍历,提取图片,下载

def main(offset,keyword):

json=get_page(offset,keyword)

for item in get_images(json):

print(item)

save_image(item)

if __name__ == '__main__':

keyword=input("请输入要爬取图片的关键词:")

offset=input("请输入要爬取的数量:")

main(offset,keyword)

一键复制

编辑

Web IDE

原始数据

按行查看

历史

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是用 C# 爬取 http://www.cnena.com/brand/list-htm-fid-9.html 的企业数据的示例代码: ```csharp using System; using System.Collections.Generic; using System.Net.Http; using System.Text.RegularExpressions; using System.Threading.Tasks; using HtmlAgilityPack; namespace Crawler { class Program { static async Task Main(string[] args) { string url = "http://www.cnena.com/brand/list-htm-fid-9.html"; HttpClient httpClient = new HttpClient(); HttpResponseMessage response = await httpClient.GetAsync(url); string content = await response.Content.ReadAsStringAsync(); HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(content); List<Dictionary<string, string>> companies = new List<Dictionary<string, string>>(); foreach (HtmlNode item in doc.DocumentNode.SelectNodes("//div[@class='company']")) { string name = item.SelectSingleNode("./h3").InnerText; string link = item.SelectSingleNode("./a").GetAttributeValue("href", ""); string address = Regex.Match(item.InnerText, @"地址:(.*)").Groups[1].Value; string tel = Regex.Match(item.InnerText, @"电话:(.*)").Groups[1].Value; companies.Add(new Dictionary<string, string> { { "name", name }, { "link", link }, { "address", address }, { "tel", tel } }); } foreach (var company in companies) { Console.WriteLine(company["name"]); Console.WriteLine(company["link"]); Console.WriteLine(company["address"]); Console.WriteLine(company["tel"]); Console.WriteLine(); } } } } ``` 这段代码使用 HttpClient 类获取网页内容,使用 HtmlAgilityPack 库解析网页,然后使用正则表达式提取相关信息。同样,你可以根据需要对代码进行修改和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值