【问题标题】:Extract Title+links from homepage从主页中提取标题+链接
【发布时间】:2013-11-21 04:40:59
【问题描述】:

我想用 python 制作我自己的 RSS

是否可以从 hdarea.org 仅提取标题和下载链接(“上传”)

这是code example

这就是我到目前为止所做的事情

import urllib2
from BeautifulSoup import BeautifulSoup
import re

page = urllib2.urlopen("http://hd-area.org").read()
soup = BeautifulSoup(page)

for title in soup.findAll("div", {"class" : "title"}):
    print (title.getText())
for a in soup.findAll('a'):
  if 'Uploaded.net' in a:
    print a['href']

它已经提取了标题。

但我卡在应该提取链接的位置。

它提取但顺序不正确...

我如何确保脚本首先检查“标题”和“链接”是否在这个 div 类中的任何建议"<div class="topbox">"

编辑

现在完成了

这是最终代码

谢谢大家 - 把我推向正确的方向

import urllib2
from BeautifulSoup import BeautifulSoup 
import datetime
import PyRSS2Gen

print "top_rls"
page = urllib2.urlopen("http://hd-area.org/index.php?s=Cinedubs").read()
soup = BeautifulSoup(page)
movieTit = []
movieLink = []
for title in soup.findAll("div", {"class" : "title"}):
    movieTit.append(title.getText())

for span in soup.findAll('span', attrs={"style":"display:inline;"},recursive=True):
    for a in span.findAll('a'):            
        if 'ploaded' in a.getText():
            movieLink.append(a['href'])
        elif 'cloudzer' in a.getText():
            movieLink.append(a['href'])

for i in range(len(movieTit)):
    print movieTit[i]
    print movieLink[i]

rss = PyRSS2Gen.RSS2(
    title = "HD-Area Cinedubs",
    link = "http://hd-area.org/index.php?s=Cinedubs",
    description = " "
                  " ",

    lastBuildDate = datetime.datetime.now(),
    items = [
       PyRSS2Gen.RSSItem(
         title = movieTit[0],
         link = movieLink[0]),
       PyRSS2Gen.RSSItem(
         title = movieTit[1],
         link = movieLink[1]),
       PyRSS2Gen.RSSItem(
         title = movieTit[2],
         link = movieLink[2]),
       PyRSS2Gen.RSSItem(
         title = movieTit[3],
         link = movieLink[3]),
       PyRSS2Gen.RSSItem(
         title = movieTit[4],
         link = movieLink[4]),
       PyRSS2Gen.RSSItem(
         title = movieTit[5],
         link = movieLink[5]),
       PyRSS2Gen.RSSItem(
         title = movieTit[6],
         link = movieLink[6]),
       PyRSS2Gen.RSSItem(
         title = movieTit[7],
         link = movieLink[7]),
       PyRSS2Gen.RSSItem(
         title = movieTit[8],
         link = movieLink[8]),
       PyRSS2Gen.RSSItem(
         title = movieTit[9],
         link = movieLink[9]),
    ])

rss.write_xml(open("cinedubs.xml", "w"))

【问题讨论】:

  • 什么意思:顺序不对?
  • 是的。我想这就是我想说的我糟糕的英语:)
  • 哦,我的意思是:你说的顺序不对是什么意思?
  • 当您访问 hd-area.org 时,每部电影都有 2 个下载链接。我抓取的每个条目都应该产生 1title+1downloadlink 等等......交替方式。现在它不这样做了。首先它会抓取所有标题而不是所有下载链接

标签: python rss


【解决方案1】:

然后这样:

movieTit = []
movieLink = []

for title in soup.findAll("div", {"class" : "title"}):
    movieTit.append(title.getText())
for a in soup.findAll('a'):
    if 'ploaded' in a.getText():
        movieLink.append(a['href'])

for i in range(0,len(movieTit)/2,2):
    print movieTit[i]
    print movieTit[i+1]
    print movieLink[i]
    print movieLink[i+1]

【讨论】:

  • 鉴于每部电影有 2 个标题和 2 个链接,我重写了那个 for 循环
  • 看起来像它的工作...我不知道为什么,但前 2 个链接不适合电影。是否可以从第一部电影的第三个链接开始?
  • 知道了。 +2。像 pi 一样简单 ;)
  • 我刚刚看到的。有时他们会更改域扩展名。我怎么能忽略它?上传*???
  • 将 if 更改为: if 'ploaded' in a.getText() 这样你也可以跳过有时它可以是大写的 U 或不是。
【解决方案2】:

一个建议,如果先找到所有的

<div class="topbox">

如果页面中有不止一个这样的。您可以像这样使用 find_all 函数或 find 函数:

soup = BeautifulSoup(page)

# in case you want to find all of them
for item in soup.find_all('div', _class='topbox'):
    # in this line you have to check where is the title : <span>, <a> or other
    # check if the tag exist or not
    if item.span is not None: 
       title = item.span.text

    # the same for this
    if item.a is not None:
        link = item.a['href']

我没有在页面中找到您想要的 div。如果你还需要,请告诉我你到底想要什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多