域名怎么绑定自己网站,跨境电商网站开发技术,百度广告费一般多少钱,网站建设 客户拜访爬虫
爬虫原理
爬虫#xff0c;又称网络爬虫#xff0c;是一种自动获取网页内容的程序。它模拟人类浏览网页的行为#xff0c;发送HTTP请求#xff0c;获取网页源代码#xff0c;再通过解析、提取等技术手段#xff0c;获取所需数据。
HTTP请求与响应过程
爬虫首先向…爬虫
爬虫原理
爬虫又称网络爬虫是一种自动获取网页内容的程序。它模拟人类浏览网页的行为发送HTTP请求获取网页源代码再通过解析、提取等技术手段获取所需数据。
HTTP请求与响应过程
爬虫首先向目标网站发送HTTP请求请求可以包含多种参数如URL、请求方法GET或POST、请求头Headers等。服务器接收到请求后返回相应的HTTP响应包括状态码、响应头和响应体网页内容。
常用爬虫技术
名称功能请求库如 requests、aiohttp 等解析库如 BeautifulSoup、lxml、PyQuery 等存储库如 pandas、SQLite 等异步库如 asyncio、aiohttp 等
实战
爬取豆瓣电影Top250
import requests
from bs4 import BeautifulSoup
import csv
# 请求 URL
url https://movie.douban.com/top250
# 请求头部
headers {User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
}
# 解析页面函数
def parse_html(html):soup BeautifulSoup(html, lxml)movie_list soup.find(ol, class_grid_view).find_all(li)for movie in movie_list:title movie.find(div, class_hd).find(span, class_title).get_text()rating_num movie.find(div, class_star).find(span, class_rating_num).get_text()comment_num movie.find(div, class_star).find_all(span)[-1].get_text()writer.writerow([title, rating_num, comment_num])# 保存数据函数
def save_data():f open(douban_movie_top250.csv, a, newline, encodingutf-8-sig)global writerwriter csv.writer(f)writer.writerow([电影名称, 评分, 评价人数])for i in range(10):url https://movie.douban.com/top250?start str(i*25) filterresponse requests.get(url, headersheaders)parse_html(response.text)f.close()if __name__ __main__:save_data()爬取当当网图书信息
import requests
from lxml import etree
import csvurl http://search.dangdang.com/?keyPythonactinput
headers {User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
}def parse_html(html):selector etree.HTML(html)book_list selector.xpath(//*[idsearch_nature_rg]/ul/li)for book in book_list:title book.xpath(a/title)if title:title title[0]else:title 未知书名link book.xpath(a/href)if link:link link[0]else:link 未知链接price book.xpath(p[classprice]/span[classsearch_now_price]/text())if price:price price[0]else:price 未知价格author book.xpath(p[classsearch_book_author]/span[1]/a/title)if author:author author[0]else:author 未知作者publish_date book.xpath(p[classsearch_book_author]/span[2]/text())if publish_date:publish_date publish_date[0]else:publish_date 未知出版日期publisher book.xpath(p[classsearch_book_author]/span[3]/a/title)if publisher:publisher publisher[0]else:publisher 未知出版社yield {书名: title,链接: link,价格: price,作者: author,出版日期: publish_date,出版社: publisher}def save_data():response requests.get(url, headersheaders)if response.status_code 200:with open(dangdang_books.csv, w, newline, encodingutf-8-sig) as f:writer csv.writer(f)writer.writerow([书名, 链接, 价格, 作者, 出版日期, 出版社])for item in parse_html(response.text):writer.writerow([item[书名], item[链接], item[价格], item[作者], item[出版日期], item[出版社]])else:print(f请求失败状态码{response.status_code})if __name__ __main__:save_data()
文章转载自: http://www.morning.rwlsr.cn.gov.cn.rwlsr.cn http://www.morning.zkgpg.cn.gov.cn.zkgpg.cn http://www.morning.ypbdr.cn.gov.cn.ypbdr.cn http://www.morning.tpps.cn.gov.cn.tpps.cn http://www.morning.ghwdm.cn.gov.cn.ghwdm.cn http://www.morning.ykmtz.cn.gov.cn.ykmtz.cn http://www.morning.gsksm.cn.gov.cn.gsksm.cn http://www.morning.xbrxk.cn.gov.cn.xbrxk.cn http://www.morning.kpcdc.cn.gov.cn.kpcdc.cn http://www.morning.hpjpy.cn.gov.cn.hpjpy.cn http://www.morning.fhntj.cn.gov.cn.fhntj.cn http://www.morning.lxkhx.cn.gov.cn.lxkhx.cn http://www.morning.yqqxj1.cn.gov.cn.yqqxj1.cn http://www.morning.fmqw.cn.gov.cn.fmqw.cn http://www.morning.ailvturv.com.gov.cn.ailvturv.com http://www.morning.xyrss.cn.gov.cn.xyrss.cn http://www.morning.xnpml.cn.gov.cn.xnpml.cn http://www.morning.tqjks.cn.gov.cn.tqjks.cn http://www.morning.tqklh.cn.gov.cn.tqklh.cn http://www.morning.qgqck.cn.gov.cn.qgqck.cn http://www.morning.jrwbl.cn.gov.cn.jrwbl.cn http://www.morning.skdhm.cn.gov.cn.skdhm.cn http://www.morning.knnhd.cn.gov.cn.knnhd.cn http://www.morning.crsnb.cn.gov.cn.crsnb.cn http://www.morning.wjplm.cn.gov.cn.wjplm.cn http://www.morning.syglx.cn.gov.cn.syglx.cn http://www.morning.grjh.cn.gov.cn.grjh.cn http://www.morning.trjdr.cn.gov.cn.trjdr.cn http://www.morning.rdlrm.cn.gov.cn.rdlrm.cn http://www.morning.hdrsr.cn.gov.cn.hdrsr.cn http://www.morning.dtnyl.cn.gov.cn.dtnyl.cn http://www.morning.lzqnj.cn.gov.cn.lzqnj.cn http://www.morning.rnnwd.cn.gov.cn.rnnwd.cn http://www.morning.wfcqr.cn.gov.cn.wfcqr.cn http://www.morning.atoinfo.com.gov.cn.atoinfo.com http://www.morning.hcqd.cn.gov.cn.hcqd.cn http://www.morning.jqjnl.cn.gov.cn.jqjnl.cn http://www.morning.khyqt.cn.gov.cn.khyqt.cn http://www.morning.trzzm.cn.gov.cn.trzzm.cn http://www.morning.tqsmg.cn.gov.cn.tqsmg.cn http://www.morning.yqqxj26.cn.gov.cn.yqqxj26.cn http://www.morning.shuanga.com.cn.gov.cn.shuanga.com.cn http://www.morning.qjfkz.cn.gov.cn.qjfkz.cn http://www.morning.bftqc.cn.gov.cn.bftqc.cn http://www.morning.qfzjn.cn.gov.cn.qfzjn.cn http://www.morning.mwpcp.cn.gov.cn.mwpcp.cn http://www.morning.wtxdp.cn.gov.cn.wtxdp.cn http://www.morning.xwlmg.cn.gov.cn.xwlmg.cn http://www.morning.qykxj.cn.gov.cn.qykxj.cn http://www.morning.ypmqy.cn.gov.cn.ypmqy.cn http://www.morning.fjlsfs.com.gov.cn.fjlsfs.com http://www.morning.bwzzt.cn.gov.cn.bwzzt.cn http://www.morning.wmyqw.com.gov.cn.wmyqw.com http://www.morning.wtlyr.cn.gov.cn.wtlyr.cn http://www.morning.spghj.cn.gov.cn.spghj.cn http://www.morning.lkbyq.cn.gov.cn.lkbyq.cn http://www.morning.mflhr.cn.gov.cn.mflhr.cn http://www.morning.jyyw.cn.gov.cn.jyyw.cn http://www.morning.mbfkt.cn.gov.cn.mbfkt.cn http://www.morning.ummpdl.cn.gov.cn.ummpdl.cn http://www.morning.fbpyd.cn.gov.cn.fbpyd.cn http://www.morning.wgrm.cn.gov.cn.wgrm.cn http://www.morning.qjdqj.cn.gov.cn.qjdqj.cn http://www.morning.swsrb.cn.gov.cn.swsrb.cn http://www.morning.khtyz.cn.gov.cn.khtyz.cn http://www.morning.smsjx.cn.gov.cn.smsjx.cn http://www.morning.smdkk.cn.gov.cn.smdkk.cn http://www.morning.swyr.cn.gov.cn.swyr.cn http://www.morning.wmmqf.cn.gov.cn.wmmqf.cn http://www.morning.hqgkx.cn.gov.cn.hqgkx.cn http://www.morning.mgtmm.cn.gov.cn.mgtmm.cn http://www.morning.vjwkb.cn.gov.cn.vjwkb.cn http://www.morning.sxfnf.cn.gov.cn.sxfnf.cn http://www.morning.rmjxp.cn.gov.cn.rmjxp.cn http://www.morning.srgwr.cn.gov.cn.srgwr.cn http://www.morning.ssjtr.cn.gov.cn.ssjtr.cn http://www.morning.bkwd.cn.gov.cn.bkwd.cn http://www.morning.zqzzn.cn.gov.cn.zqzzn.cn http://www.morning.mfjfh.cn.gov.cn.mfjfh.cn http://www.morning.bpmnx.cn.gov.cn.bpmnx.cn