Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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在不同的网页上循环_Python_Loops_Csv_Spyder - Fatal编程技术网

使用Python在不同的网页上循环

使用Python在不同的网页上循环,python,loops,csv,spyder,Python,Loops,Csv,Spyder,我目前正在学习一门关于大数据的课程,但对它了解不多。对于作业,我想了解TripAdvisor论坛上讨论的阿姆斯特丹主题。我想创建一个CSV文件,包括主题、作者和每个主题的回复量。一些问题: a如何列出所有主题?我检查了所有页面的网站源代码,主题总是在'onclick=“setPID(34603)”后面陈述,并以结尾。我尝试了”(re.findall(r'onclick=“setPID(34603)”>(.*?“,post)”,但它不起作用 回复不在commentsection中给出,而是在页面上

我目前正在学习一门关于大数据的课程,但对它了解不多。对于作业,我想了解TripAdvisor论坛上讨论的阿姆斯特丹主题。我想创建一个CSV文件,包括主题、作者和每个主题的回复量。一些问题:

  • a如何列出所有主题?我检查了所有页面的网站源代码,主题总是在
    'onclick=“setPID(34603)”
    后面陈述,并以
    结尾。我尝试了
    ”(re.findall(r'onclick=“setPID(34603)”>(.*?“
    ,post)”,但它不起作用
  • 回复不在commentsection中给出,而是在页面上的单独一行中给出。如何循环并将所有回复附加到新变量
  • 如何循环前20页?代码中的URL仅包括第一页,提供20个主题
  • 我是在循环之前还是之后创建CSV文件
  • 这是我的密码:

    from urllib import request
    import re
    import csv
    
    topiclist=[]
    metalist=[]
    
    req = request.Request('https://www.tripadvisor.com/ShowForum-g188590-i60- 
    Amsterdam_North_Holland_Province.html', headers={'User-Agent' : 
    "Mozilla/5.0"})
    
    tekst=request.urlopen(req).read()
    tekst=tekst.decode(encoding="utf-8",errors="ignore").replace("\n"," ")
    .replace("\t"," ")
    
    
    topicsection=re.findall(r'<b><a(.*?)</div>',tekst)
    
    topic=[]
    for post in topicsection:
       topic.append(re.findall(r'onclick="setPID(34603)">(.*?)</a>', post)
    
    
    author=[]
    for post in topicsection: 
       author.append(re.findall(r'<a href="/members-forums/.*?">(.*?)</a>', 
       post))
    
    replies=re.findall(r'<td class="reply rowentry.*?">(.*?)</td>',tekst)
    
    来自urllib导入请求
    进口稀土
    导入csv
    主题列表=[]
    metalist=[]
    req=请求。请求('https://www.tripadvisor.com/ShowForum-g188590-i60- 
    Amsterdam_North_Holland_Province.html',headers={'User-Agent':
    “Mozilla/5.0”})
    tekst=request.urlopen(req.read)()
    tekst=tekst.decode(encoding=“utf-8”,errors=“ignore”)。替换(“\n”,”)
    .替换(“\t”,”)
    topicsection=re.findall(r“”,
    邮政(
    回复=re.findall(r'(.*?),tekst)
    
    使用html解析器,如beautifulsoup

    e、 g-

    使用html解析器,如beautifulsoup

    e、 g-


    请参阅并了解添加链接的原因!我之前实际上在寻找第一个链接。这是一个非常好的答案。非常感谢!我确实也尝试了BeautifulSoup,但使用了另一个代码。当我打印主题时,我确实可以看到它们,但仍然可以在输出中获得主题的URL。例如:可能吗e是否只打印“阿姆斯特丹艺术节的味道”?只需在BS对象上调用
    .text
    。因此,如果您在
    主题上迭代:
    主题:打印(topic.text)
    这确实有效,太好了!使用BeautifulSou在不同页面之间循环也是最好的方法吗?我尝试了这里所解释的方法:但它似乎不起作用,并且出于添加链接的原因!我实际上是在寻找第一个。这是一个非常好的答案。非常感谢!我确实尝试了BeautifulSoup也是,但使用另一个代码。当我打印主题时,我确实可以看到它们,但仍然可以在输出中获得主题的URL。例如:是否可以只打印“阿姆斯特丹节的味道”?只需在BS对象上调用
    。text
    。因此,如果您迭代
    主题
    主题中的主题:打印(主题.文本)
    这确实有效,太好了!使用Beautifulsoup在不同页面之间循环是否也是最好的方法?我尝试了这里的解释:但它似乎没有我不想说的那么有效,如果你在抓取网页,你可能会很幸运地使用
    xml.dom
    我不想说的那样,如果你在抓取网页使用
    xml.dom
    from bs4 import BeautifulSoup
    import requests
    
    r = requests.get("https://www.tripadvisor.com/ShowForum-g188590-i60-Amsterdam_North_Holland_Province.html")
    soup = BeautifulSoup(r.content, "html.parser") #or another parser such as lxml
    topics = soup.find_all("a", {'onclick': 'setPID(34603)'})
    #do stuff