19.2 Python入门之编写爬虫实战

准备:


Beatiful Soup库:该库可以从HTML或XML文件中提取数据,通过转换器实现常规的文档导航,查找,修改等操作,该库需要安装后使用


目标:


编写爬虫,爬取百度百科“网络爬虫”的词条("http://baike.baidu.com/view/284853.htm"),并将所有包含“view”关键字的链接按格式打印出来


实现过程:


首先使用前先使用urllib.request模块从指定网址上读取HTML文件


>>>import urllib.request

>>>from bs4 import BeatifulSoup

>>>url = "http://baike.baidu.com/view/284853.htm"

>>>response = urllib.request.urlopen(url)

>>>html = response.read()

>>>soup = BeatifulSoup(html,"html.parser")


BeatifulSoup需要两个参数,第一个参数是所提取数据的所在HTML或XML文件,第二个参数是指定解析器,然后使用find_all(href = re.compile("view"))方法来读取所有包含“view”关键字的链接(使用正则表达式知识),使用for语句迭代读取


>>>import re

>>>for each in soup.find_all(href = re.compile("view")):#???

     print(each.text,"- >","".join(["http://baike.baidu.com",\each["href"]]))


最终代码为


for each in soup.find_all(href = re.compile("view")):#???

     print(each.text,"- >","".join(["http://baike.baidu.com",each["href"]]))



import urllib.request

from bs4 import BeatifulSoup

import re


def main():

  url = "http://baike.baidu.com/view/284853.htm"

  response = urllib.request.urlopen(url)

  html = response.read()

  soup = BeatifulSoup(html,"html.parser")


  for each in soup.find_all(href = re.compile("view")):#???

     print(each.text,"- >","".join(["http://baike.baidu.com",each["href"]]))


 if _ _name_ _ == "_ _main_ _": #只有单独运行该.py文件才会执行main()

    main()



目标:爬虫接收用户输入的关键词,进入每一个词条,然后检测该词条是否有副标题,若有,则打印出来


代码清单


import urllib.request

import urllib.parse

from bs4 import BeatifulSoup

import re


def main():

  keyword = input("请输入关键词:")

  keyword = urllib.parse.urlencode({"word":keyword}) #?

  response = \

  urllib.request.urlopen("http://baike.baidu.com/search/word?%s"%\

  keyword)

  html = response.read()

  soup = BeatifulSoup(html,"html.parser")  #操作文件中数据,如查找等

 

  for each in soup.find_all(href = re.compile("view")):

    content = ''.join([each.text])

    ur12 = ''.join(["http://baike.baidu.com",each["href"]])

    response2 = urllib.request.urlopen(ur12)

    html2 = response2.read()

    soup2 = BeatifulSoup(html2,"html.parser")

    if soup2.h2:

       content = ''.join([content,soup2.h2.text])

    content = ''.join([content,"->",ur12])

    print(content)


 if _ _name_ _ == "_ _main_ _": #只有单独运行该.py文件才会执行main()

    main()


上述程序某些语句语法待继续学习








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您想了解如何使用Python编写PowerPoint脚本自动化,是吗?那么我可以给您一些指导。 首先,您需要安装Python的PPTX库,它可以帮助您创建、修改和保存PowerPoint文档。您可以使用以下命令在命令行中安装该库: ``` pip install python-pptx ``` 然后,您可以使用以下代码创建一个简单的PowerPoint文档: ```python from pptx import Presentation prs = Presentation() slide = prs.slides.add_slide(prs.slide_layouts[0]) title = slide.shapes.title subtitle = slide.placeholders[1] title.text = "Hello, World!" subtitle.text = "Using Python to automate PowerPoint" prs.save("presentation.pptx") ``` 这个代码将创建一个只有一个标题和子标题的PowerPoint幻灯片,并将其保存为“presentation.pptx”文件。 您可以使用PPTX库的其他功能来自定义幻灯片的布局、添加图像、表格、图表等。例如,以下代码将创建一个包含图表和表格的幻灯片: ```python from pptx import Presentation from pptx.chart.data import ChartData from pptx.enum.chart import XL_CHART_TYPE from pptx.util import Inches prs = Presentation() slide = prs.slides.add_slide(prs.slide_layouts[5]) chart_data = ChartData() chart_data.categories = ['East', 'West', 'Midwest'] chart_data.add_series('Series 1', (19.2, 21.4, 16.7)) x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5) chart = slide.shapes.add_chart( XL_CHART_TYPE.COLUMN_CLUSTERED, x, y, cx, cy, chart_data ).chart table = slide.shapes.add_table(2, 2, Inches(4), Inches(2), Inches(4), Inches(2)).table table.cell(0, 0).text = 'Name' table.cell(0, 1).text = 'Age' table.cell(1, 0).text = 'John' table.cell(1, 1).text = '25' prs.save("presentation.pptx") ``` 这个代码将创建一个包含一个柱状图和一个表格的幻灯片,并将其保存为“presentation.pptx”文件。 希望这些代码可以帮助您开始使用Python自动化PowerPoint。如果您有任何问题,请随时问我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值