python爬虫例子
『壹』 python新手关于爬虫的简单例子
# coding:utf-8
from bs4 import BeautifulSoup
import requests
import os
url = 'http://www..com'
r = requests.get(url)
demo = r.text # 服务器返回响应
soup = BeautifulSoup(demo, "html.parser")
"""
demo 表示被解析的html格式的内容
html.parser表示解析用的解析器
"""
# 输出响应的html对象
ab = list()
with open("D:\\temp\\mii.txt","w+",encoding="utf-8") as xxx:
for mi in soup.find_all('a'):
# ab.append(mi.prettify()) # 使用prettify()格式化显示输出
xxx.writelines(str(mi))
xxx.write("\n")
xxx.close()
执行完毕 D盘下 temp 目录的 mii.txt文件会得到爬取到的所有链接。
『贰』 如何利用python写爬虫程序
利用python写爬虫程序的方法:
1、先分析网站内容,红色部分即专是网站文章内容div。
『叁』 如何利用python写爬虫程序
首先,你要安装requests和BeautifulSoup4,然后执行如下代码.='.parser')#标题H1=soup.select('#artibodyTitle')[0].text#来源time_source=soup.select('.time-source')[0].text#来源origin=soup.select('#artibodyp')[0].text.strip()#原标题oriTitle=soup.select('#artibodyp')[1].text.strip()#内容raw_content=soup.select('#artibodyp')[2:19]content=[]forparagraphinraw_content:content.append(paragraph.text.strip())'@内'.join(content)#责任编辑ae=soup.select('.article-editor')[0].text这样容就可以了
『肆』 求用Python编写的一个简单的网络爬虫,跪求!!!!
#爬虫的需求:爬取github上有关python的优质项目
#coding=utf-8
importrequests
frombs4importBeautifulSoup
defget_effect_data(data):
results=list()
soup=BeautifulSoup(data,'html.parser')
#printsoup
projects=soup.find_all('div',class_='repo-list-itemd-flexflex-justify-startpy-4publicsource')
forprojectinprojects:
#printproject,'----'
try:
writer_project=project.find('a',attrs={'class':'v-align-middle'})['href'].strip()
project_language=project.find('div',attrs={'class':'d-table-cellcol-2text-graypt-2'}).get_text().strip()
project_starts=project.find('a',attrs={'class':'muted-link'}).get_text().strip()
update_desc=project.find('p',attrs={'class':'f6text-graymr-3mb-0mt-2'}).get_text().strip()
#update_desc=None
result=(writer_project.split('/')[1],writer_project.split('/')[2],project_language,project_starts,update_desc)
results.append(result)
exceptException,e:
pass
#printresults
returnresults
defget_response_data(page):
request_url='https://github.com/search'
params={'o':'desc','q':'python','s':'stars','type':'Repositories','p':page}
resp=requests.get(request_url,params)
returnresp.text
if__name__=='__main__':
total_page=1#爬虫数据的总页数
datas=list()
forpageinrange(total_page):
res_data=get_response_data(page+1)
data=get_effect_data(res_data)
datas+=data
foriindatas:
printi
『伍』 如何用python写出爬虫
写出爬虫实际上没有你想象的那么难,就这3步:
定义item类
开发spider类(核心)
开发pipeline
如果你想要更详细的内容,我推荐这本很容易理解的书:《疯狂Python讲义》
『陆』 亿牛云提供的python爬虫示例使用报407
示例代码方便提供下吗?
『柒』 求亿牛云爬虫代理python的代码示例
你用的是python的哪个模块,每个模块引入的方式不同
『捌』 利用python爬虫技术可以做到哪些炫酷有趣的事
用python爬虫爬取股票公司网站数据,做短线买股票会提高你买入和抛售的判断。
『玖』 python新手求助 关于爬虫的简单例子
#coding=utf-8
from bs4 import BeautifulSoup
with open('index.html', 'r') as file:
fcontent = file.read()
sp = BeautifulSoup(fcontent, 'html.parser')
t = 'new_text_for_replacement'
# replace the paragraph using `replace_with` method
sp.find(itemprop='someprop').replace_with(t)
# open another file for writing
with open('output.html', 'w') as fp:
# write the current soup content
fp.write(sp.prettify())
如果要替换段落的内容而不是段落元素本身,可以设置.string属性。
sp.find(itemprop='someprop').string = t
赞0收藏0评论0分享
用户回答回答于 2018-07-26
问题取决于你搜索标准的方式,尝试更改以下代码:
print(sp.replace(sp.find(itemprop="someprop").text,t))
对此:
print(sp.replace(sp.find({"itemprop":"someprop"}).text,t))
# coding:utf-8
from bs4 import BeautifulSoup
import requests
import os
url = 'https://'
r = requests.get(url)
demo = r.text # 服务器返回响应
soup = BeautifulSoup(demo, "html.parser")
"""
demo 表示被解析的html格式的内容
html.parser表示解析用的解析器
"""
# 输出响应的html对象
ab = list()
with open("D:\\temp\\mii.txt","w+",encoding="utf-8") as xxx:
for mi in soup.find_all('a'):
ab.append(mi.prettify()) # 使用prettify()格式化显示输出
# xxx.writelines(str(mi))
xxx.writelines(ab)
xxx.close()
『拾』 Python爬虫如何写
有专门的教程,在网络资源里搜一下。