python html_使用Python格式化HTML代码

1586010002-jmsa.png

I have a list of URLs in a column in a CSV-file. I would like to use Python to go through all the URLs, download a specific part of the HTML code from the URL and save it to the next column.

For example:

From this URL I would like to extract this div and write it to the next column.

VM−2N ist ein Hochleistungs−Verteilverstärker für Composite− oder SDI−Videosignale und unsymmetrisches Stereo−Audio. Das Eingangssignal wird entkoppelt und isoliert, anschließend wird das Signal an zwei identische Ausgänge verteilt.

Hohe Bandbreite — 400 MHz (–3 dB).

Desktop–Grösse — Kompakte Bauform, zwei Geräte können mithilfe des optionalen Rackadapters RK–1 in einem 19 Zoll Rack auf 1 HE nebeneinander montiert werden.

I have this code, the HTML code is saved in the variable html:

import csv

import urllib.request

with open("urls.csv", "r", newline="", encoding="cp1252") as f_input:

csv_reader = csv.reader(f_input, delimiter=";", quotechar="|")

header = next(csv_reader)

items = [row[0] for row in csv_reader]

with open("results.csv", "w", newline="") as f_output:

csv_writer = csv.writer(f_output, delimiter=";")

for item in items:

html = urllib.request.urlopen(item).read()

Currently the HTML-Code is pretty ugly. How could I delete everything out of the variable html except the div I would like to extract?

解决方案

Given that all of your webpages are have the same structure you can parse the html with this code. It will look for the first div with the id product_bullets_section. An id in html should be unique but the given website has two equal id's so we obtain the first one through slicing and convert the parsed div back to a string containing your html.

import csv

import urllib.request

from bs4 import BeautifulSoup

with open("urls.csv", "r", newline="", encoding="cp1252") as f_input:

csv_reader = csv.reader(f_input, delimiter=";", quotechar="|")

header = next(csv_reader)

items = [row[0] for row in csv_reader]

items = ['https://www.kramerav.com/de/Product/VM-2N']

with open("results.csv", "w", newline="") as f_output:

csv_writer = csv.writer(f_output, delimiter=";")

for item in items:

html = urllib.request.urlopen(item).read()

the_div = str(BeautifulSoup(html).select('div#product_bullets_section')[0])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值