beautifulsoup

Find previous occurrence of an element

我是研究僧i 提交于 2020-12-05 07:05:45
问题 I have the following html: <h4>Testing</h4> <h3>Test</h3> <h3>Test2</h3> <h4>Testing2</h4> If I have the element <h3>Test2</h3> referenced in a variable, how can I find <h4>Testing</h4> ? The one before the referenced element, not after. 回答1: Use .previous_sibling: element.previous_sibling Or, .find_previous_sibling() to explicitly find the first preceding h4 tag: element.find_previous_sibling('h4') Demo: >>> from bs4 import BeautifulSoup >>> data = """ ... <h4>Testing</h4> ... <h3>Test</h3>

python3爬取”理财大视野”中的股票,并分别写入txt、excel和mysql

泄露秘密 提交于 2020-12-04 10:55:07
需求: 爬取“ 理财大视野 ”网站的排名、代码、名称、市净率、市盈率等信息,并分别写入txt、excel和mysql 环境:python3.6.5 网站: http://www.dashiyetouzi.com/tools/value/Graham.php 查看html源码:信息在html中以table形式存在,每个股票信息是一行,存放在tr中,单元格信息存放在td中 因此思路为:通过id或者class查找table→查找tr→查找td 第三方库 1 from bs4 import BeautifulSoup 2 from urllib import request 3 import time 4 import xlrd 5 import xlwt 6 import pymysql 获取html源码 1 url = " http://www.dashiyetouzi.com/tools/value/Graham.php " 2 htmlData = request.urlopen(url).read().decode( ' utf-8 ' ) 3 soup = BeautifulSoup(htmlData, ' lxml ' ) 4 # print(soup.prettify()) 5 allData = soup.find( " table " , { ' class ' : '

Python爬虫进阶之Scrapy

[亡魂溺海] 提交于 2020-12-04 05:36:55
用Scrapy爬取百度图片 前段时间用python的requests库和BeautifulSoup库爬取了猫眼电影关于柯南剧场版的6000条评论 这次我们来使用Scrapy框架来实现爬虫任务——百度“唯美图片”的爬取 整个项目的工程源码我已经上传到GitHub上了,感兴趣的同学可以自行下载,能顺便给我的项目一个star那再好不过了 项目地址:https://github.com/ITBoy-China/scrapy 先展示下我们爬取的结果 看着爬取下来的这一张一张的图,内心的满满的成就感有没有,哈哈,那接下来就跟着我一起来看看如何去实现图片的爬取吧。 一、准备工作 我们此次用到的工具有: python3.7.3 PyCharm5.0.3 Scrapy1.7.4 没有安装scrapy的直接在命令行里pip install scrapy安装scrapy框架,在windows环境下安装scrapy开始会报错,这是因为安装scrapy要安装其它的一些依赖库,lxml、pyOpenSSL、Twisted 、pywin32。 安装好这些库之后,再去安装scrapy就不会报错了。 安装完成之后我们在命令行里输入scrapy看是否安装成功,结果如下: 然后我们开始创建Scrapy项目,在命令行输入: scrapy startproject XXX 其中XXX表示的是你的项目名称

python爬虫的基本框架

和自甴很熟 提交于 2020-12-04 04:01:52
1.爬虫的基本流程: 通过requests库的get方法获得网站的url 浏览器打开网页源码分析元素节点 通过BeautifulSoup或者正则表达式提取想要的数据 储存数据到本地磁盘或者数据库 2.正式开工啦 url = “http://www.jianshu.com” page = requests.get(url) #发现返回状态码403,说明有问题出现(除200外,其他的都是有问题的) #这个时候查看一下爬虫的robots协议,的确有些问题,解决方案如下: headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'} 获取html页面 page = requests.get(url, headers = headers) demo = page.text #记住,有时候有可能出现编码问题 page.encoding = page.apparent_encoding #将获取的内容转换为BeautifulSoup格式,并将html.parser作为解释器(熬一锅汤) soup = BeautifulSoup(demo, 'html.parser')

菜鸟学IT之python网页爬取初体验

余生颓废 提交于 2020-12-04 01:46:40
作业来源: https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/2881 1. 简单说明爬虫原理 爬虫简单来说就是通过程序模拟浏览器放松请求站点的行为,把站点返回的HTML代码/JSON数据/二进制数据(图片、视频) 爬到本地,通过一些算法进而提取自己需要的数据,存放起来使用。 2. 理解爬虫开发过程 1).简要说明浏览器工作原理; 2).使用 requests 库抓取网站数据; requests.get(url) 获取校园新闻首页html代码 import requests url = ' http://news.gzcc.cn/html/2019/xiaoyuanxinwen_0320/11029.html ' res = requests.get(url) type(res) res.encoding = ' utf-8 ' soupn = BeautifulSoup(res.text, ' html.parser ' ) # html 规格打印 print (soupn) 截图: 3).了解网页 写一个简单的html文件,包含多个标签,类,id html_sample = ' \ <html> \ <body> \ <h1 id="title">Hello</h1> \ <a href="#" class=

Python爬虫框架

会有一股神秘感。 提交于 2020-12-02 03:38:32
本文章的源代码来源于 https://github.com/Holit/Web-Crawler-Framwork 一、爬虫框架的代码 1 import urllib.request 2 from bs4 import BeautifulSoup 3 import re 4 import time 5 import _thread 6 7 # Input your Url here#################################### 8 BaseURL = ' 127.0.0.1/ ' 9 # ######################################################## 10 TaxURL = " .html " 11 12 # Input your data-saving path ############################ 13 SavePath = "" 14 # ######################################################## 15 16 # Input your threads count ############################### 17 thread_count = 1 18 # ############################

8个最高效的Python爬虫框架,你用过几个?

*爱你&永不变心* 提交于 2020-12-02 01:30:34
来源:云栖社区 1.Scrapy Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架。可以应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中。。用这个框架可以轻松爬下来如亚马逊商品信息之类的数据。 项目地址:https://scrapy.org/ 2.PySpider pyspider 是一个用python实现的功能强大的网络爬虫系统,能在浏览器界面上进行脚本的编写,功能的调度和爬取结果的实时查看,后端使用常用的数据库进行爬取结果的存储,还能定时设置任务与任务优先级等。 项目地址:https://github.com/binux/pyspider 3.Crawley Crawley可以高速爬取对应网站的内容,支持关系和非关系数据库,数据可以导出为JSON、XML等。 项目地址:http://project.crawley-cloud.com/ 4.Portia Portia是一个开源可视化爬虫工具,可让您在不需要任何编程知识的情况下爬取网站!简单地注释您感兴趣的页面,Portia将创建一个蜘蛛来从类似的页面提取数据。 项目地址:https://github.com/scrapinghub/portia 5.Newspaper Newspaper可以用来提取新闻、文章和内容分析。使用多线程,支持10多种语言等。 项目地址:https://github.com

How to parse the website using Beautifulsoup

僤鯓⒐⒋嵵緔 提交于 2020-11-28 02:45:53
问题 I am new to web scraping and i want to get the html of the page.But when i run the program i get html empty and console show the javascript from bs4 import BeautifulSoup import requests import urllib url = "https://linkedin.com/company/1005" r = requests.get(url) html_content = r.text soup = BeautifulSoup(html_content,'html.parser') print (soup.prettify()) 回答1: Problem is not BeautifulSoup but server which needs more information in requests to give you access to this page. Now it sends

How to parse the website using Beautifulsoup

做~自己de王妃 提交于 2020-11-28 02:45:22
问题 I am new to web scraping and i want to get the html of the page.But when i run the program i get html empty and console show the javascript from bs4 import BeautifulSoup import requests import urllib url = "https://linkedin.com/company/1005" r = requests.get(url) html_content = r.text soup = BeautifulSoup(html_content,'html.parser') print (soup.prettify()) 回答1: Problem is not BeautifulSoup but server which needs more information in requests to give you access to this page. Now it sends

How to parse the website using Beautifulsoup

烈酒焚心 提交于 2020-11-28 02:44:29
问题 I am new to web scraping and i want to get the html of the page.But when i run the program i get html empty and console show the javascript from bs4 import BeautifulSoup import requests import urllib url = "https://linkedin.com/company/1005" r = requests.get(url) html_content = r.text soup = BeautifulSoup(html_content,'html.parser') print (soup.prettify()) 回答1: Problem is not BeautifulSoup but server which needs more information in requests to give you access to this page. Now it sends