【问题标题】:Display images and titles in 2 column in python在 python 的 2 列中显示图像和标题
【发布时间】:2015-11-10 22:21:57
【问题描述】:

我将所有标题和图片源链接抓取到一个文本文件中,然后使用文本文件中的数据输出一个包含 2 列的 html 文件,一列用于图像,一列用于标题。如何显示可点击的图像,并显示标题和2列格式的图像?这是我所拥有的

from bs4 import BeautifulSoup

titles = []
images = []
href  = []

r = urllib.urlopen('https://www.open2study.com/courses').read()
soup = BeautifulSoup(r)

for i in soup.find_all('div', {'class': "courses_adblock_rollover"}):
    titles.append(i.h2.text)

for i in soup.find_all('img', {'class': "image-style-course-logo-subjects-block"}):
    images.append(i.get('src'))



with open('test.txt', "w") as f:
    for i in zip(titles, images):
        f.write(i[0].encode('ascii', 'ignore') + '\n'
                +i[1].encode('ascii', 'ignore') +
                '\n\n')

header = '<!doctyle html><html><head><title>My page</title></head><body>'
body = '<table><tr><td></td><td></td></tr>'

footer = '</table></body></html>'


with open('test.txt', 'r') as input, open('test.html', 'w') as output:
   output.write(header)
   output.write(body)

   for line in input:
    #ignore blank lines
       if line == '\n':
            continue

       col1 = line.rstrip()
       #read next line
       col2 = next(input).rstrip()
       output.write('<tr><td>{}</td><td><img src="{}" style="width: 160px;             height: 100px"></td></tr>\n\n'.format(col1, col2))
       output.write(footer)

【问题讨论】:

  • 您的previous question 出了什么问题?
  • 图片和标题两栏不显示,图片不可点击
  • 或基本的html。首先将其映射到 HTML 中,然后使用代码构建该结构。

标签: python html beautifulsoup


【解决方案1】:

我觉得你让这个刮擦的东西对你自己来说真的很困难。首先从最大的元素开始更容易,即整个课程 div,然后稍后从中提取信息。

此代码为您提供第一列中的可点击图像和第二列中的课程标题。

from bs4 import BeautifulSoup
import urllib

base_url = 'https://www.open2study.com'
r = urllib.urlopen(base_url + '/courses').read()

soup = BeautifulSoup(r, "html.parser")

courses = soup.find_all('div', {'class': "courses_adblock_start"})

encoding = "utf-8"
page_title = 'Ai Truong'
html_template = '<!doctyle html><html><head><title>{}</title><meta charset="{}" /></head><body>{}</body></html>'
table_template = '<table>{}</table>'
table_row_template = '<tr><td>{}</td><td>{}</td></tr>'
img_template = '<a href="{}"><img src="{}" width="160px;" alt="{}"></a>'

table_rows = ''
for c in courses:
    title = c.h2.text.encode(encoding)
    image = c.find('img', {'class': 'image-style-course-logo-subjects-block'}).get('src')
    href = c.parent.get('href')
    img_tag = img_template.format(base_url + href, image, title)
    table_rows += table_row_template.format(img_tag, title)

table_tag = table_template.format(table_rows)

with open('course-scrape.html', 'w') as html_out:
    html_out.write(html_template.format(page_title, encoding, table_tag))

输出

【讨论】:

  • 我运行你的代码得到了这个:image = c.find('img', class_='image-style-course-logo-subjects-block').get('src') AttributeError : 'NoneType' 对象没有属性 'get'
  • 我只是在本地运行它并且该行有效...我假设您使用的是最新的 BeautifulSoup 版本? @米娜
  • 我的 mac 上运行 beautifulsoup 时遇到问题。是的,我得到了最新版本,但它只能在 python2 上运行
  • 如果你这样做了怎么办image = c.find('img', {'class': 'image-style-course-logo-subjects-block'}).get('src')
  • 太棒了。更新了答案。
猜你喜欢
  • 2018-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多