标签: beautifulsoup

Python - 使用 BeautifulSoup4 获取父元素的第一个子元素

我有以下 HTML:

<table id="mytable">
    <tr role="row">
        <td>abc1</td>
        <td>abc2</td>
        <td>abc3</td>
        <td class="hm">Data1</td>
        <td>Data2</td>
        <td class="hm">no</td>
        <td class="hx">yes</td>
        <td class="hm">Updated</td>
    </tr>
    <tr role="row">
        <td>def1</td>
        <td>def2</td>
        <td>def3</td>
        <td class="hm">Data3</td>
        <td>Data4</td>
        <td class="hm">no</td>
        <td class="hx">no</td>
        <td class="hm">Updated</td>
    </tr>
    <tr role="row">
        <td>hij1</td>
        <td>hij2</td>
        <td>hij3</td>
        <td class="hm">Data5</td>
        <td>Data6</td>
        <td class="hm">no</td>
        <td class="hx">no</td>
        <td class="hm">Updated</td>
    </tr>
    <tr role="row">
        <td>klm1</td>
        <td>klm2</td>
        <td>klm3</td>
        <td class="hm">Data7</td>
        <td>Data8</td>
        <td class="hm">no</td>
        <td class="hx">yes</td>
        <td class="hm">Updated</td>
    </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

这些标签有多个<tr>,因此我正在执行以下操作来查找其中包含子<td>标签class="hx"且文本为 的标签yes

if (Soup.find('table', {'id' : 'mytable'}).find('td', …
Run Code Online (Sandbox Code Playgroud)

html python beautifulsoup parent-child

2
推荐指数
1
解决办法
5030
查看次数

在 Scrapy 中利用 Beautifulsoup

我创建了一个简单的爬虫,Scrapy它从给定的链接开始,并跟踪给定内的所有链接,DEPTH_LIMIT每次运行蜘蛛时都会根据项目参数进行调整。为了简单起见,该脚本会打印响应 URL。

import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
from NONPROF.items import NonprofItem
from scrapy.http import Request
import re

class Nonprof(CrawlSpider):
    name = "my_scraper"
    allowed_domains = ["stackoverflow.com"]
    start_urls = ["https://stackoverflow.com"]

    rules = [
        Rule(LinkExtractor(
            allow=['.*']),
             callback='parse_item',
             follow=True)
        ]

    def parse_item (self, response):
        print (response.url)
Run Code Online (Sandbox Code Playgroud)

我当前的目标是解析从起始 URL 开始的给定深度内的所有可见文本,并使用该数据进行主题建模。我过去使用 做过类似的事情BeautifulSoup,但我想在我的爬虫中利用以下解析语言。

from bs4 import BeautifulSoup
import bs4 as bs
import urllib.request

def tag_visible(element):
    if element.parent.name in ['style', 'script', 'head', 'title', 'meta', '[document]']:
        return …
Run Code Online (Sandbox Code Playgroud)

python parsing beautifulsoup scrapy

2
推荐指数
1
解决办法
5618
查看次数

在 Tkinter 的文本小部件中添加文本链接

我正在使用 Tkinter 和 bs4 在 python 中创建一个歌曲通知程序。我已经从网站中提取了歌曲及其相应的网址。我使用文本小部件来存储歌曲并将其网址作为字典中的键值。

现在我想添加歌曲名称的链接(存储在文本小部件中),以便当我单击特定歌曲时,它的网址会在 chrome 中打开。

这是代码片段:

from tkinter import *
import webbrowser
from bollywood_top_50 import bollywood_songs_list , bollywood_songs_dict
from international_top_50 import international_songs_list


b_songs_list  = bollywood_songs_list()
b_songs_dict =  bollywood_songs_dict()
i_songs_list = international_songs_list()

root = Tk()
S = Scrollbar(root)
T = Text(root, height=20, width=30,cursor="hand2")
S.pack(side=RIGHT, fill=Y)
T.pack(side=LEFT, fill=Y)
S.config(command=T.yview)
T.config(yscrollcommand=S.set)    


def callback_a():
    T.delete(1.0,END)
    for songs in b_songs_list:
       T.insert(END, songs + '\n')   

def callback_b():
    T.delete(1.0,END)
    for songs in i_songs_list:
        T.insert(END, songs + '\n')        

bollywood_button = Button(root,text="Bollywood-Top-50", command=callback_a)
bollywood_button.pack() …
Run Code Online (Sandbox Code Playgroud)

tkinter beautifulsoup python-3.x

2
推荐指数
1
解决办法
3749
查看次数

无法获取英文搜索结果

我用 python 编写了一个脚本来执行谷歌搜索并获取结果。目前工作正常。然而,我面临的唯一问题是,我得到的大部分结果都是我的母语,而不是英语,而当我在谷歌浏览器中执行相同的搜索时,我得到的是英语结果。

我如何修改我的脚本以获得所有英文搜索结果?

这是我到目前为止的尝试:

from bs4 import BeautifulSoup
import requests

link = "http://www.google.com/search?q={}"

def fetch_results(query):
    res = requests.get(link.format(query.replace(" ","+")))
    soup = BeautifulSoup(res.text,"lxml")
    for item in soup.select("span"):
        print(item.get_text())

if __name__ == '__main__':
    fetch_results('india')
Run Code Online (Sandbox Code Playgroud)

python beautifulsoup web-scraping python-3.x

2
推荐指数
1
解决办法
493
查看次数

美丽的汤无法加载整个页面

我有一个网络爬行脚本

import requests
from lxml import html
import bs4
res = requests.get('https://in.linkedin.com/in/ASAMPLEUSERNAME', headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'})

print(res.text)
Run Code Online (Sandbox Code Playgroud)

请将代码中的 url 中的“ASAMPLEUSERNAME”替换为一些虚拟 linkedin 用户

但代码只给了我部分不完整(几乎没有)的网页源

python beautifulsoup web-crawler

2
推荐指数
1
解决办法
1万
查看次数

使用Python3和BeautifulSoup解析xml文件

我知道有关使用 Python 3 解析 xml 的问题有几个答案,但我找不到两个答案。我正在尝试从 BoardGameGeek xml 文件中解析和提取信息,如下所示(它太长了,无法粘贴到此处):

\n\n

https://www.boardgamegeek.com/xmlapi/boardgame/10

\n\n

1)我无法从这两行中提取主要游戏名称:

\n\n
<name sortindex="1" primary="true">Elfenland</name>\n<name sortindex="1">Elfenland (\xd0\x92\xd0\xbe\xd0\xbb\xd1\x88\xd0\xb5\xd0\xb1\xd0\xbd\xd0\xbe\xd0\xb5 \xd0\x9f\xd1\x83\xd1\x82\xd0\xb5\xd1\x88\xd0\xb5\xd1\x81\xd1\x82\xd0\xb2\xd0\xb8\xd0\xb5)</name>\n
Run Code Online (Sandbox Code Playgroud)\n\n

2)我在提取数据列表时也遇到问题,例如在这个xml中:

\n\n
<poll title="User Suggested Number of Players" totalvotes="96"  name="suggested_numplayers">\n    <results numplayers="1">\n        <result numvotes="0" value="Best"/>\n        <result numvotes="0" value="Recommended"/>\n        <result numvotes="58" value="Not Recommended"/>\n    </results>\n    <results numplayers="2">\n        <result numvotes="2" value="Best"/>\n        <result numvotes="21" value="Recommended"/>\n        <result numvotes="53" value="Not Recommended"/>\n    </results>\n    <results numplayers="3">\n        <result numvotes="10" value="Best"/>\n        <result numvotes="46" value="Recommended"/>\n        <result numvotes="17" value="Not Recommended"/>\n    </results>\n        <results numplayers="4">\n        <result numvotes="47" value="Best"/>\n        <result numvotes="36" value="Recommended"/>\n        <result numvotes="1" …
Run Code Online (Sandbox Code Playgroud)

xml beautifulsoup python-3.x

2
推荐指数
1
解决办法
6964
查看次数

在 Python 3 中使用 BeautifulSoup 抓取 URL

我尝试了这段代码,但包含 URL 的列表仍为空。没有错误按摩,什么都没有。

from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
import re

req = Request('https://www.metacritic.com/browse/movies/genre/date?page=0', headers={'User-Agent': 'Mozilla/5.0'})
html_page = urlopen(req).read()

soup = BeautifulSoup(html_page, features="xml")
links = []
for link in soup.findAll('a', attrs={'href': re.compile("^https://www.metacritic.com/movie/")}):
    links.append(link.get('href'))

print(links)
Run Code Online (Sandbox Code Playgroud)

我想抓取在给定 URL“ https://www.metacritic.com/browse/movies/genre/date ? ”中找到的所有以“ https://www.metacritic.com/movie/”开头的 URL?页=0 “。

我究竟做错了什么?

python urllib beautifulsoup python-3.x

2
推荐指数
1
解决办法
7461
查看次数

request.get() 被卡住了

您好,我正在尝试从网站上抓取一些数据,但 request.get() 遇到了一些问题。这是我的代码:

page_url = front_end+str(i)+'/'
page = requests.get(page_url)
Run Code Online (Sandbox Code Playgroud)

所以我希望它是一个字符串,因为我只是输入一个网址,如果我停止代码或者它运行太长时间,我会得到类似的内容:

File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", 
line 377, in _make_request
    httplib_response = conn.getresponse(buffering=True)
TypeError: getresponse() got an unexpected keyword argument 'buffering'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 24, in <module>
    page = requests.get(page_url)
  File "/usr/local/lib/python3.6/site-packages/requests/api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/requests/api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 533, in request
    resp = self.send(prep, …
Run Code Online (Sandbox Code Playgroud)

python beautifulsoup web-scraping python-requests

2
推荐指数
1
解决办法
4754
查看次数

BeautifulSoup 查找类包含一些特定的单词

我已经四处搜索以查找如何查找名称包含某些单词的类,但我没有找到它。我想从名为单词页脚的类中获取信息。

<div class="footerinfo"> <span class="footerinfo__header"> </span> </div>

<div class="footer">
    <div class="w-container container-footer">
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

我已经尝试过,但仍然不起作用

soup.find_all('div',class_='^footer^'):
Run Code Online (Sandbox Code Playgroud)

 soup.find_all('div',class_='footer*'):
Run Code Online (Sandbox Code Playgroud)

有人有这样做的想法吗?

beautifulsoup web-scraping

2
推荐指数
1
解决办法
6610
查看次数

如何在Python中比较列表中的多个日期?

我想知道如何比较列表中的日期。我想提取“最早”的日期。(我做了一个 for 循环,因为我必须用“-”替换一些字符)

comment_list = comment_container.findAll("div", {"class" : "comment-date"})
D =[]

  for commentDate in comment_list:
    year, month, day = map(int, commentDate.split('-'))
    date_object = datetime(year, month, day)
    date_object = datetime.strptime(commentDate, '%Y-%m-%d').strftime('%Y-%m-%d')   
    D.append(date_object)

print(D)
Run Code Online (Sandbox Code Playgroud)

输出:

['2018-06-26', '2018-04-01', '2018-07-19', '2018-04-23', '2018-08-25', '2018-06-08', '2018-06-14', '2018-07-08', '2019-03-15', '2019-03-15', '2019-03-15', '2019-03-15', '2019-03-15']
Run Code Online (Sandbox Code Playgroud)

我想提取最早的日期:

例如。

'2018-04-01'

python list beautifulsoup web-scraping

2
推荐指数
1
解决办法
1503
查看次数