爬虫python代码-python爬虫(附源码)

声明:本文内容皆来自网上

环境:ubuntu19.04、python3.x

python包:requests、bs4、beautifulsoup、re、urllib、lxml、os

下载方式:$pip install [包名]

ps:部分电脑未安装python-pip,报错后按照系统提示下载python-pip4aa545dccf7de8d4a93c2b2b8e3265ac0a26d216.png

爬虫过程:

1)模拟浏览器向目标网页发送请求

2)接收响应

3)解析,将响应转为网页代码输出

4)查找代码中需要的部分

5)处理

代码实现:

这里以爬取发表情(https://www.fabiaoqing.com/biaoqing)网页上的图片为例

1、2) url = 'https://www.fabiaoqing.com/biaoqing' #目标网址

response = requests.get(url)   #发送访问请求接收

3)  soup= BeautifulSoup(response.content.decode('utf-8'), 'lxml')  #解析响应,此时soup是目标网页代码

4)  gowl = str(soup.findAll('img'))  #查找所有图片,gowl即为图片url

#由于我们仅需要其中的表情,所以需要对gowl再进行一次筛选。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package test; import java.io.IOException; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Queue; import org.htmlparser.Node; import org.htmlparser.Parser; import org.htmlparser.Tag; import org.htmlparser.util.NodeIterator; import org.htmlparser.util.NodeList; import org.htmlparser.util.ParserException; public class Spider implements Runnable { boolean search_key_words = false; int count = 0; int limitsite = 10; int countsite = 1; String keyword = "中国"; Parser parser = new Parser(); // List linkList=new ArrayList(); String startsite = ""; SearchResultBean srb; List resultList = new ArrayList(); List searchsite = new ArrayList(); Queue linklist = new LinkedList(); HashMap<String, ArrayList<String>> disallowListCade = new HashMap<String, ArrayList<String>>(); public Spider(String keyword, String startsite) { this.keyword = keyword; this.startsite = startsite; linklist.add(startsite); srb = new SearchResultBean(); } public void run() { } public void search(Queue queue) { String url = ""; while (!queue.isEmpty()) { url = queue.peek().toString(); try { if (!isSearched(searchsite, url)) { if (isRobotAllowed(new URL(url))) { processHtml(url); } } else { System.out.println("thissssssss"); } } catch (Exception e) { e.printStackTrace(); } queue.remove(); } } private void processHtml(String url) throws Exception { searchsite.add(url); count = 0; System.out.println("searching..." + url); try { parser.setURL(url); parser.setEncoding("gbk"); URLConnection uc = parser.getConnection(); uc.connect(); uc.getLastModified(); NodeIterator nit = parser.elements(); while (nit.hasMoreNodes()) { Node node = nit.nextNode(); parserNode(node); } srb.setKeyWords(keyword); srb.setUrl(url); srb.setCount_key_words(count); resultList.add(srb); System.out.println("keywords:" + count); } catch (ParserException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void dealTag(Tag tag) throws Exception { NodeList list = tag.getChildren(); if (list != null) { NodeIterator it = list.elements(); while (it.hasMoreNodes()) { Node node = it.nextNode(); parserNode(node); } } } private void parserNode(Node node) throws Exception { if (node instanceof StringNode) { StringNode n = (StringNode) node; } } private boolean isRobotAllowed(URL url) { return false; } private boolean isSearched(List searchsite2, String url) { return false; } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值