Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.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 2.7中,如何使用BeautifulSoup和请求从TD标记之间提取文本_Python_Beautifulsoup - Fatal编程技术网

在Python 2.7中,如何使用BeautifulSoup和请求从TD标记之间提取文本

在Python 2.7中,如何使用BeautifulSoup和请求从TD标记之间提取文本,python,beautifulsoup,Python,Beautifulsoup,我正在尝试使用BeautifulSoup从TD标记和Python 2.7中的请求之间提取文本。到目前为止,使用此代码我什么都没有得到:( 导入请求 从bs4导入BeautifulSoup #安装蜘蛛 def卡搜索(最多页面): 页码=1 mtgset='portal' 卡片=‘熔岩斧’ 而page如果你想在抓取时有更大的灵活性,你需要像phantomJs这样的东西。看看Pykler的anwser。似乎你需要传递标题。当我获取该URL时,我根本没有得到任何包含表的内容。我不知道URL是否错误(我注

我正在尝试使用BeautifulSoup从TD标记和Python 2.7中的请求之间提取文本。到目前为止,使用此代码我什么都没有得到:(

导入请求
从bs4导入BeautifulSoup
#安装蜘蛛
def卡搜索(最多页面):
页码=1
mtgset='portal'
卡片=‘熔岩斧’

而page如果你想在抓取时有更大的灵活性,你需要像phantomJs这样的东西。看看Pykler的anwser。

似乎你需要传递标题。当我获取该URL时,我根本没有得到任何包含表的内容。我不知道URL是否错误(我注意到您没有在URL中的任何位置使用
mtgset
page
变量…),您应该在正文中使用一些信息而不是GET来执行POST,您正在运行深层链接保护或类似功能,或者该表是由JavaScript生成的,而不是页面的静态部分。但无论如何,如果该表不存在,则无法解析该表。这与您的Python代码无关;如果我只需将相同的URL传递给curl或wget,我得到的是同一个页面,其中没有表。因此,如果您真的要将链接从表中拉出,则需要发布实际执行此操作的代码,而不是不执行此操作的代码。Abarnert,没有使用正确的mtgset和card变量。page用于在while循环中计数。如果是否由Javascript生成,是否有其他方法提取数据?Abarnet,我编辑了原始帖子,以显示从页面提取链接的代码。但是,提取链接的代码不是针对表的。只是页面本身。感谢所有响应的人。带Selenium的PhantomJs看起来像票。
import requests
from bs4 import BeautifulSoup

# Set up the Spider

def card_search(max_pages):
    page = 1
    mtgset = 'portal'
    card = 'lava-axe'

    while page <= max_pages:
        url = 'http://www.mtgotraders.com/store/search-results.html?q=lava+axe&x=0&y=0'
        source_code = requests.get(url)
        plain_text = source_code.text
        soup = BeautifulSoup(plain_text)

        for text in soup.findAll('td',{'class': 'price mod'}):
            pagetext = text.get('td')

            print(pagetext)
            page += 1

card_search(1)
import requests
from bs4 import BeautifulSoup

# Set up the Spider

def card_search(max_pages):
    page = 1
    mtgset = 'portal'
    card = 'lava-axe'

    while page <= max_pages:
        url = 'http://www.mtgotraders.com/store/search-results.html?q=lava+axe&x=0&y=0'
        source_code = requests.get(url)
        plain_text = source_code.text
        soup = BeautifulSoup(plain_text)

        for text in soup.findAll('a'):
            pagetext = text.get('href')

            print(pagetext)
            page += 1

card_search(1)