Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用BeautifulSoup从reddit中删除表链接_Python_Python 3.x_Beautifulsoup - Fatal编程技术网

Python 如何使用BeautifulSoup从reddit中删除表链接

Python 如何使用BeautifulSoup从reddit中删除表链接,python,python-3.x,beautifulsoup,Python,Python 3.x,Beautifulsoup,我正在尝试使用Beautiful Soup从Reddit表中获取链接,并且可以成功提取表中除URL以外的所有内容。我正在使用项。find_all('a')但使用此代码时返回一个空列表: import praw import csv import requests from bs4 import BeautifulSoup def Authorize(): """Authorizes Reddit API""" reddit = praw.Reddit(client_id='',

我正在尝试使用Beautiful Soup从Reddit表中获取链接,并且可以成功提取表中除URL以外的所有内容。我正在使用
项。find_all('a')
但使用此代码时返回一个空列表:

import praw
import csv
import requests
from bs4 import BeautifulSoup

def Authorize():
    """Authorizes Reddit API"""
    reddit = praw.Reddit(client_id='',
                     client_secret='',
                     username='',
                     password='',
                     user_agent='user')

url = 'https://old.reddit.com/r/formattesting/comments/94nc49/will_it_work/'
headers = {'User-Agent': 'Mozilla/5.0'}
page = requests.get(url, headers=headers)

soup = BeautifulSoup(page.text, 'html.parser')

table_extract = soup.find_all('table')[0]
table_extract_items = table_extract.find_all('a')

for item in table_extract_items:
    letter_name = item.contents[0]
    links = item.find_all('a')
    print(letter_name)
    print(links)
这就是它的回报:

6GB EVGA GTX 980 TI
[]
Intel i7-4790K
[]
Asus Z97-K Motherboard
[]
2x8 HyperX Fury DDR3 RAM
[]
Elagto HD 60 Pro Capture Card
[]
我想有一个网址,其中空列表是下面的每一个表行


我不确定这是否会对构造产生影响,但最终目标是提取所有表内容和链接(保持两者之间的关联),并将其作为两列保存到CSV。但是现在我只是想把它打印出来以保持简单。

你就快到了。您的
表\u extract\u项
是HTML锚定,您需要从中提取
文本
——使用
[
]
运算符提取内容和属性
href
。我猜变量名称的不当选择让你感到困惑。loop
links=item.find_all('a')
中的行错误

以下是我的解决方案:

for anchor in table.findAll('a'):
    # if not anchor: finaAll returns empty list, .find() return None
    #    continue
    href = anchor['href']    
    print (href)
    print (anchor.text)
我的代码中的
table
是您在代码中命名的
table\u extract

选中此项:

In [40]: for anchor in table.findAll('a'):
        # if not anchor:
        #        continue
        href = anchor['href']
        text = anchor.text
        print (href, "--", text)
   ....:     
https://imgur.com/a/Y1WlDiK -- 6GB EVGA GTX 980 TI
https://imgur.com/gallery/yxkPF3g -- Intel i7-4790K
https://imgur.com/gallery/nUKnya3 -- Asus Z97-K Motherboard
https://imgur.com/gallery/9YIU19P -- 2x8 HyperX Fury DDR3 RAM
https://imgur.com/gallery/pNqXC2z -- Elagto HD 60 Pro Capture Card
https://imgur.com/gallery/5K3bqMp -- Samsung EVO 250 GB SSD
https://imgur.com/FO8JoQO -- Corsair Scimtar MMO Mouse
https://imgur.com/C8PFsX0 -- Corsair K70 RGB Rapidfire Keyboard
https://imgur.com/hfCEzMA -- I messed up

您正在寻找Imgur图像的链接吗?