Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.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 使用For Loop和BeautifulSoup选择不同URL上的文本_Python_Beautifulsoup - Fatal编程技术网

Python 使用For Loop和BeautifulSoup选择不同URL上的文本

Python 使用For Loop和BeautifulSoup选择不同URL上的文本,python,beautifulsoup,Python,Beautifulsoup,近4天来,我一直在绞尽脑汁,试图找到最好的方法,在一个网站的URL表中循环,请求URL,并从第二个网站的两个不同区域刮取文本 我已经多次尝试重写这个脚本,使用了几种不同的解决方案来实现我想要的结果,但是,我还没有完全完成 目前,我可以选择第一页上表格的第一个链接,转到新页面并选择所需的数据,但我无法获得代码以继续循环第一页上的每个链接 import requests from bs4 import BeautifulSoup journal_site = "https://journals.s

近4天来,我一直在绞尽脑汁,试图找到最好的方法,在一个网站的URL表中循环,请求URL,并从第二个网站的两个不同区域刮取文本

我已经多次尝试重写这个脚本,使用了几种不同的解决方案来实现我想要的结果,但是,我还没有完全完成

目前,我可以选择第一页上表格的第一个链接,转到新页面并选择所需的数据,但我无法获得代码以继续循环第一页上的每个链接

import requests
from bs4 import BeautifulSoup

journal_site = "https://journals.sagepub.com"
site_link 'http://journals.sagepub.com/action/showPublications?
pageSize=100&startPage='

# each page contains 100 results I need to scrape from 
page_1 = '0'
page_2 = '1'
page_3 = '3'
page_4 = '4'

journal_list = site_link + page_1
r = requests.get(journal_list)
soup = BeautifulSoup(r.text, 'html.parser')

for table_row in soup.select('div.results'):
    journal_name = table_row.findAll('tr', class_='False')
    journal_link = table_row.find('a')['href']
    journal_page = journal_site + journal_link

    r = requests.get(journal_page)
    soup = BeautifulSoup(r.text, 'html.parser')

    for journal_header, journal_description in zip(soup.select('main'), 
    soup.select('div.journalCarouselTextText')):
        try:
            title = journal_header.h1.text.strip()
            description = journal_description.p.text.strip()
            print(title,':', description)
        except AttributeError:
            continue

找到每个期刊名称的标题和描述的最佳方法是什么?提前感谢您的帮助

您的大多数代码对我都有效,只需修改代码的中间部分,使前后部分保持不变:

# all code same up to here

journal_list = site_link + page_1
r = requests.get(journal_list)
soup = BeautifulSoup(r.text, 'html.parser')

results = soup.find("div", { "class" : "results" })

table = results.find('table')

for row in table.find_all('a', href=True):
    journal_link = row['href']
    journal_page = journal_site + journal_link

    # from here same as your code
在它从第一页得到100个结果的第四个响应(标题/描述)后,我停止了。我很确定它会得到所有预期的结果,只需要在随后的4页中循环


希望这有帮助。

非常感谢!你的过程现在已经很清楚了,我很感激你的细节!