我在for循环中得到相同的输出

huangapple go评论50阅读模式
英文:

I get the same output in for loop

问题

The Hartley Clinic Clinic Contact: Ailing Jeavons 01256 856289 , , Fleet, RG27 8NZ Monday 8:30 — 17:00 Tuesday 8:30 — 19:00 Wednesday 8:30— 17:00 Thursday 8:30 — 17:00 Friday 8:30 — 15:00 Saturday 9:00 — 17:00 Sunday Closed https://www.saveface.co.uk/clinic/the-hartley-clinic/
The Hartley Clinic Clinic Contact: Ailing Jeavons 01256 856289 , , Fleet, RG27 8NZ Monday 8:30 — 17:00 Tuesday 8:30 — 19:00 Wednesday 8:30— 17:00 Thursday 8:30 — 17:00 Friday 8:30 — 15:00 Saturday 9:00 — 17:00 Sunday Closed https://www.saveface.co.uk/clinic/the-hartley-clinic/
The Hartley Clinic Clinic Contact: Ailing Jeavons 01256 856289 , , Fleet, RG27 8NZ Monday 8:30 — 17:00 Tuesday 8:30 — 19:00 Wednesday 8:30— 17:00 Thursday 8:30 — 17:00 Friday 8:30 — 15:00 Saturday 9:00 — 17:00 Sunday Closed https://www.saveface.co.uk/clinic/the-hartley-clinic/
The Hartley Clinic Clinic Contact: Ailing Jeavons 01256 856289 , , Fleet, RG27 8NZ Monday 8:30 — 17:00 Tuesday 8:30 — 19:00 Wednesday 8:30— 17:00 Thursday 8:30 — 17:00 Friday 8:30 — 15:00 Saturday 9:00 — 17:00 Sunday Closed https://www.saveface.co.uk/clinic/the-hartley-clinic/
The Hartley Clinic Clinic Contact: Ailing Jeavons 01256 856289 , , Fleet, RG27 8NZ Monday 8:30 — 17:00 Tuesday 8:30 — 19:00 Wednesday 8:30— 17:00 Thursday 8:30 — 17:00 Friday 8:30 — 15:00 Saturday 9:00 — 17:00 Sunday Closed https://www.saveface.co.uk/clinic/the-hartley-clinic/
The Hartley Clinic Clinic Contact: Ailing Jeavons 01256 856289 , , Fleet, RG27 8NZ Monday 8:30 — 17:00 Tuesday 8:30 — 19:00 Wednesday 8:30— 17:00 Thursday 8:30 — 17:00 Friday 8:30 — 15:00 Saturday 9:00 — 17:00 Sunday Closed https://www.saveface.co.uk/clinic/the-hartley-clinic/
英文:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
import pandas as pd

s=Service("C:\selenium driver\chromedriver.exe")
driver = webdriver.Chrome(service=s)

companies_names = []
persons_names = []
phones_numbers = []
locations = []
opening_hours = []
descriptions = []
websites_links = []
all_profiles = []

driver.get("https://www.saveface.co.uk/search/")

driver.implicitly_wait(10)

blocks = driver.find_elements(By.XPATH, "//div[@class='result clientresult']")

for block in range(30):
    
    company_name = blocks[block].find_element(By.XPATH, "//h3[@class='resulttitle']").text.strip()
    companies_names.append(company_name)

    person_name = blocks[block].find_element(By.XPATH, "//p[@class='name_wrapper']").text.strip()
    persons_names.append(person_name)

    phone_number = blocks[block].find_element(By.XPATH, "//div[@class='searchContact phone']").text.strip()
    phones_numbers.append(phone_number)

    location = blocks[block].find_element(By.XPATH, "//li[@class='cls_loc']").text.strip()
    locations.append(location)

    opening_hour = blocks[block].find_element(By.XPATH, "//li[@class='opening-hours']").text.strip()
    opening_hours.append(opening_hour)

    profile = blocks[block].find_element(By.XPATH, "//a[@class='visitpage']").get_attribute("href")
    all_profiles.append(profile)
    
    print(company_name, person_name, phone_number, location, opening_hour, profile)

    if block == 29:
        two_page = driver.find_element(By.XPATH, "//a[@class='facetwp-page']")
        two_page.click()
        
        driver.implicitly_wait(10)

        blocks = driver.find_elements(By.XPATH, "//div[@class='result clientresult']")

for i in range(len(all_profiles)):
    
    driver.get(all_profiles[i])

    description = driver.find_element(By.XPATH, "//div[@class='desc-text-left']").text.strip()
    descriptions.append(description)

    website_link = driver.find_element(By.XPATH, "//a[@class='visitwebsite website']").get_attribute("href")
    websites_links.append(website_link)        


driver.implicitly_wait(10)
driver.close()

df = pd.DataFrame(
    {
        "company_name": companies_names,
        "person_name": persons_names,
        "phone_number": phones_numbers,
        "location": locations,
        "opening_hour": opening_hours,
        "description": descriptions,
        "website_link": websites_links,
        "profile_on_saveface": all_profiles
    }
)

df.to_csv('saveface.csv',index=False)
#print(df)

This is the result:

The Hartley Clinic Clinic Contact: Ailing Jeavons 01256 856289 , , Fleet, RG27 8NZ Monday 8:30 — 17:00 Tuesday 8:30 — 19:00 Wednesday 8:30— 17:00 Thursday 8:30 — 17:00 Friday 8:30 — 15:00 Saturday 9:00 — 17:00 Sunday Closed https://www.saveface.co.uk/clinic/the-hartley-clinic/
The Hartley Clinic Clinic Contact: Ailing Jeavons 01256 856289 , , Fleet, RG27 8NZ Monday 8:30 — 17:00 Tuesday 8:30 — 19:00 Wednesday 8:30— 17:00 Thursday 8:30 — 17:00 Friday 8:30 — 15:00 Saturday 9:00 — 17:00 Sunday Closed https://www.saveface.co.uk/clinic/the-hartley-clinic/
The Hartley Clinic Clinic Contact: Ailing Jeavons 01256 856289 , , Fleet, RG27 8NZ Monday 8:30 — 17:00 Tuesday 8:30 — 19:00 Wednesday 8:30— 17:00 Thursday 8:30 — 17:00 Friday 8:30 — 15:00 Saturday 9:00 — 17:00 Sunday Closed https://www.saveface.co.uk/clinic/the-hartley-clinic/
The Hartley Clinic Clinic Contact: Ailing Jeavons 01256 856289 , , Fleet, RG27 8NZ Monday 8:30 — 17:00 Tuesday 8:30 — 19:00 Wednesday 8:30— 17:00 Thursday 8:30 — 17:00 Friday 8:30 — 15:00 Saturday 9:00 — 17:00 Sunday Closed https://www.saveface.co.uk/clinic/the-hartley-clinic/
The Hartley Clinic Clinic Contact: Ailing Jeavons 01256 856289 , , Fleet, RG27 8NZ Monday 8:30 — 17:00 Tuesday 8:30 — 19:00 Wednesday 8:30— 17:00 Thursday 8:30 — 17:00 Friday 8:30 — 15:00 Saturday 9:00 — 17:00 Sunday Closed https://www.saveface.co.uk/clinic/the-hartley-clinic/
The Hartley Clinic Clinic Contact: Ailing Jeavons 01256 856289 , , Fleet, RG27 8NZ Monday 8:30 — 17:00 Tuesday 8:30 — 19:00 Wednesday 8:30— 17:00 Thursday 8:30 — 17:00 Friday 8:30 — 15:00 Saturday 9:00 — 17:00 Sunday Closed https://www.saveface.co.uk/clinic/the-hartley-clinic/

答案1

得分: 1

为了将搜索限制在以上下文节点为根的子树中,您的表达式应该以.//开头,因此您需要在每个命令中用.//替换//

... = blocks[block].find_element(...)

//的意思是从文档的根开始搜索,完全忽略上下文节点blocks[block]

此外,请注意,并非所有的块都具有位置,如您可以从此图中看到:

[![enter image description here][1]][1]

在这种情况下,

location = blocks[block].find_element(By.XPATH, "//li[@class='cls_loc']")

将引发NoSuchElementException异常。为了避免这种情况,您需要将命令放在try...except...块中。

更新

使用Selenium从400个块中进行爬取在我的电脑上需要约1分钟,我尝试使用BeautifulSoup,只需要不到1秒!爬取慢的部分是爬取个人资料,因为对于每个个人资料,我们都必须下载一个新的网页,但即使使用BeautifulSoup,速度仍然要快得多。

因此,我编写了一个不使用Selenium,只使用BeautifulSoup的脚本(您可以在终端中运行pip install beautifulsoup4来安装它):

import requests
from bs4 import BeautifulSoup
url = 'https://www.saveface.co.uk/search/'
soup = BeautifulSoup(requests.get(url).text, "html.parser")

css_selector = {
    'company name' : ".title",
    'person name'  : ".name_wrapper",
    'phone number' : ".phone",
    'location'     : ".cls_loc",
    'opening hours': ".opening-hours",
    'profile link' : ".visitpage",
}
data = {key:[] for key in list(css_selector)+['description','website link']}
number_of_pages = int(str(soup).split('total_pages":')[1].split('}')[0])

for page in range(2,number_of_pages+2):
    
    blocks = soup.select('.clientresult')
    for idx,block in enumerate(blocks):
        print(f'blocks {idx+1}/{len(blocks)}',end='\r')
        for key in list(css_selector):
            try:
                if 'link' in key:
                    data[key] += [ block.select_one(css_selector[key])['href'] ]
                else:
                    data[key] += [ block.select_one(css_selector[key]).text.strip().replace('\r\n', ', ') ]
            except AttributeError:
                data[key] += ['*missing value']

    if page <= number_of pages:
        print('\nloading page', page)
        url_page = f'{url}?fwp_paged={page}&'
        soup = BeautifulSoup(requests.get(url_page).text, "html.parser")

print('\nno more pages to load, moving to scrape profile links...')

for idx,url in enumerate(data['profile link']):
    
    print(f"profile link {idx+1}/{len(data['profile link'])}  ",end='\r')
    soup_profile = BeautifulSoup(requests.get(url).text, "html.parser")
    try:
        data['description'] += [soup_profile.select_one('.clinicContent > .description').text.strip()]
    except AttributeError:
        data['description'] += ['*missing value']
    try:
        data['website link'] += [soup_profile.select_one('.visitwebsite')['href']]
    except AttributeError:
        data['website link'] += ['*missing value']

Output执行完需要约8分钟

blocks 400/400
loading page 2
blocks 109/109
no more pages to load, moving to scrape profile links...
profile link 509/509


然后,您可以轻松通过运行`pd.DataFrame(data)`来创建数据框。
<details>
<summary>英文:</summary>
To restric the search within a subtree rooted at the context node, your expression should start with `.//` so you have to replace `//` with `.//` in each of the commands
... = blocks[block].find_element(...)
The meaning of `//` is to search the document from the document&#39;s root, ignoring the context node `blocks[block]` altogether.
Moreover, notice that not all the blocks have a location as you can see from this image
[![enter image description here][1]][1]
in this case 
location = blocks[block].find_element(By.XPATH, &quot;//li[@class=&#39;cls_loc&#39;]&quot;)
will raise a `NoSuchElementException`. To avoid this you have to put the command in a `try...except...` block
# UPDATE
Scraping 400 blocks with selenium takes about 1 minute on my computer, I tried with BeautifulSoup and it just takes less than 1 second! The slow part is to scrape the profiles, because for each of them we have to download a new webpage, however is still way faster with BeautifulSoup.
So I write a script without using selenium, just BeautifulSoup (you can install by running `pip install beautifulsoup4` in the terminal)
import requests
from bs4 import BeautifulSoup
url = &#39;https://www.saveface.co.uk/search/&#39;
soup = BeautifulSoup(requests.get(url).text, &quot;html.parser&quot;)
css_selector = {
&#39;company name&#39; : &quot;.title&quot;,
&#39;person name&#39;  : &quot;.name_wrapper&quot;,
&#39;phone number&#39; : &quot;.phone&quot;,
&#39;location&#39;     : &quot;.cls_loc&quot;,
&#39;opening hours&#39;: &quot;.opening-hours&quot;,
&#39;profile link&#39; : &quot;.visitpage&quot;,
}
data = {key:[] for key in list(css_selector)+[&#39;description&#39;,&#39;website link&#39;]}
number_of_pages = int(str(soup).split(&#39;total_pages&quot;:&#39;)[1].split(&#39;}&#39;)[0])
for page in range(2,number_of_pages+2):
blocks = soup.select(&#39;.clientresult&#39;)
for idx,block in enumerate(blocks):
print(f&#39;blocks {idx+1}/{len(blocks)}&#39;,end=&#39;\r&#39;)
for key in list(css_selector):
try:
if &#39;link&#39; in key:
data[key] += [ block.select_one(css_selector[key])[&#39;href&#39;] ]
else:
data[key] += [ block.select_one(css_selector[key]).text.strip().replace(&#39;\r\n&#39;,&#39;, &#39;) ]
except AttributeError:
data[key] += [&#39;*missing value*&#39;]
if page &lt;= number_of_pages:
print(&#39;\nloading page&#39;, page)
url_page = f&#39;{url}?fwp_paged={page}&#39;
soup = BeautifulSoup(requests.get(url_page).text, &quot;html.parser&quot;)
print(&#39;\nno more pages to load, moving to scrape profile links...&#39;)
for idx,url in enumerate(data[&#39;profile link&#39;]):
print(f&quot;profile link {idx+1}/{len(data[&#39;profile link&#39;])}  &quot;,end=&#39;\r&#39;)
soup_profile = BeautifulSoup(requests.get(url).text, &quot;html.parser&quot;)
try:
data[&#39;description&#39;] += [soup_profile.select_one(&#39;.clinicContent &gt; .description&#39;).text.strip()]
except AttributeError:
data[&#39;description&#39;] += [&#39;*missing value*&#39;]
try:
data[&#39;website link&#39;] += [soup_profile.select_one(&#39;.visitwebsite&#39;)[&#39;href&#39;]]
except AttributeError:
data[&#39;website link&#39;] += [&#39;*missing value*&#39;]
Output (it took about 8 minutes to complete the execution)
blocks 400/400
loading page 2
blocks 109/109
no more pages to load, moving to scrape profile links...
profile link 509/509  
Then you can easily create the dataframe by running `pd.DataFrame(data)`
[![enter image description here][2]][2]
[1]: https://i.stack.imgur.com/RLjpLm.png
[2]: https://i.stack.imgur.com/vsGn0.png
</details>
# 答案2
**得分**: 0
这是新的代码
但为什么每一页都返回相同的输出:
<details>
<summary>英文:</summary>
this is the new code
but it returns the same output on every page why:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
import pandas as pd

s=Service("C:\selenium driver\chromedriver.exe")
driver = webdriver.Chrome(service=s)

companies_names = []
persons_names = []
phones_numbers = []
locations = []
opening_hours = []
descriptions = []
websites_links = []
all_profiles = []

driver.get("https://www.saveface.co.uk/search/")

driver.implicitly_wait(10)

pages = driver.find_elements(By.XPATH, ".//a[@class='facetwp-page']")

for page in range(len(pages)+1):

blocks = driver.find_elements(By.XPATH, &quot;.//div[@class=&#39;result clientresult&#39;]&quot;)
for block in range(10):
try:    
company_name = blocks[block].find_element(By.XPATH, &quot;.//h3[@class=&#39;resulttitle&#39;]&quot;).text.strip()
companies_names.append(company_name)
except:
companies_names.append(&quot;Not found on the site&quot;)
try:    
person_name = blocks[block].find_element(By.XPATH, &quot;.//p[@class=&#39;name_wrapper&#39;]&quot;).text.strip()
persons_names.append(person_name)
except:
persons_names.append(&quot;Not found on the site&quot;)
try:    
phone_number = blocks[block].find_element(By.XPATH, &quot;.//div[@class=&#39;searchContact phone&#39;]&quot;).text.strip()
phones_numbers.append(phone_number)
except:
phones_numbers.append(&quot;Not found on the site&quot;)
try:    
location = blocks[block].find_element(By.XPATH, &quot;.//li[@class=&#39;cls_loc&#39;]&quot;).text.strip()
locations.append(location)
except:
locations.append(&quot;Not found on the site&quot;)
try:    
opening_hour = blocks[block].find_element(By.XPATH, &quot;.//li[@class=&#39;opening-hours&#39;]&quot;).text.strip()
opening_hours.append(opening_hour)
except:
opening_hours.append(&quot;Not found on the site&quot;)
try:    
profile = blocks[block].find_element(By.XPATH, &quot;.//a[@class=&#39;visitpage&#39;]&quot;).get_attribute(&quot;href&quot;)
all_profiles.append(profile)
except:
all_profiles.append(&quot;Not found on the site&quot;)
two_page = driver.find_element(By.XPATH, &quot;.//a[@class=&#39;facetwp-page&#39;]&quot;)
two_page.click()

for i in range(len(all_profiles)):

try:
driver.get(all_profiles[i])
driver.implicitly_wait(10)
try:    
description = driver.find_element(By.XPATH, &quot;.//div[@class=&#39;desc-text-left&#39;]&quot;).text.strip()
descriptions.append(description)
except:
descriptions.append(&quot;Not found on the site&quot;)
try:    
website_link = driver.find_element(By.XPATH, &quot;.//a[@class=&#39;visitwebsite website&#39;]&quot;).get_attribute(&quot;href&quot;)
websites_links.append(website_link)        
except:
websites_links.append(&quot;Not found on the site&quot;)
except:
descriptions.append(&quot;Not found on the site&quot;)
websites_links.append(&quot;Not found on the site&quot;)

driver.implicitly_wait(10)
driver.close()

df = pd.DataFrame(
{
"company_name": companies_names,
"person_name": persons_names,
"phone_number": phones_numbers,
"location": locations,
"opening_hour": opening_hours,
"description": descriptions,
"website_link": websites_links,
"profile_on_saveface": all_profiles
}
)

df.to_csv('saveface.csv',index=False)
print(df)


</details>

huangapple
  • 本文由 发表于 2023年2月14日 21:02:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448238.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定